void __fastcall TPSM_OperatorClientForm::csktClientSocketRead
(
      TObject *             Sender          ,
      TCustomWinSocket *    Socket
)
{
    try
    {
        bool done = false;
        int bytes = 0;
        static int bytesLeft = 0;
        int msgBytes = 0;
        int offset = 0;
        struct PSM_Message *msg = 0;
    
        // Allocate memory to receive message
        static char *ptr;
        static char buf[MAX_SIZE];

        ptr = buf;

         // Read the message from the socket
        bytes = Socket->ReceiveBuf((void*)(ptr + bytesLeft), (MAX_SIZE - bytesLeft));

        // Set the bytes left value
        bytesLeft += bytes;
        WriteLog("bytes=%d   bytesLeft=%d", bytes, bytesLeft );    
        
         // Are there messages to be dispatched
        int iTotalLen = 0;

        while(1)
        {
            void *cpyBuff = 0;
        
            // Get the 1st message
            //msg = (struct PSM_Message *)&(ptr[offset]);
            msg = (struct PSM_Message *)(ptr+bytesLeft);
            msgBytes = msg->m_size;
            WriteLog("m_messageID=%d   m_size=%d", 
                        m_messageID, msg->m_size );    

            //badri
            if(bytesLeft < msgBytes)
            {
                // return;
                break;
            }
        
            // Allocate copy buffer for message
            cpyBuff = calloc(msg->m_size, sizeof(char));
            //  cpyBuff = calloc(msgBytes, sizeof(char));
        
            // cpyBuff = malloc(msgBytes);
            if( cpyBuff == NULL)
                return;
    
           // Copy the 1st message to the copy buffer
           ::memmove((void *)cpyBuff, (const void *)&(ptr[offset]), sizeof(char) * msgBytes);
           iTotalLen += sizeof(char) * msgBytes;
    
           // Move the offset
           offset += msg->m_size;
    
           if(msg->m_messageID == MSG_SM_OC_DATABASE_PATH_INFO)
           {
              WriteLog("OperatorCLientForm : Connected to server received - MSG_SM_OC_DATABASE_PATH_INFO");
              LogonRequest(msg);
              free(cpyBuff);
           }
           else
           {
                if (!::PostThreadMessage(pRouterThread->ThreadID, msg->m_messageID, (WPARAM)cpyBuff, 0))
                {
                    //Savita 11/dec/2001
                    AnsiString str = " PostMsgFailed --The Error Code returned " + AnsiString(GetLastError());
                    WriteLog(L_ERROR,str.c_str());
                    free(cpyBuff);
    
                    //If it has not deleted also create this thread inorder to maintain the continuity
                    pRouterThread = NULL;
                    pRouterThread = new PSM_RouterThread(false);

                    //Viswanath   12/dec/2001
                    if(pRouterThread != NULL)
                    {
                        WriteLog("OperatorCLientForm : Creating New Router Thread");
                    }
                    else
                    {
                        //Display a message stating that the resources is low. Shut down OC gracefully
                        //MessageBox(Handle,"System running low on resources.....Shutting down Operator Client","MMPOSEM Error",MB_OK |MB_SYSTEMMODAL );
                        MessageBox(Handle,GetString(IDS_ERR_SYSTEM_LOW_RESOURCES).c_str(),
                                        GetString(IDS_ERR_IPOS_ERROR).c_str(),
                                        MB_OK |MB_SYSTEMMODAL );
                        exit(-1);
                    }
                }
            }
        
            // adjust the bytes left value
            bytesLeft -= msg->m_size;
            if (bytesLeft < 0)   //if bytesLeft is less than zero
            {
                WriteLog("size of m_size is @ line 6833 %d", sizeof(msg->m_size));
                bytesLeft = 0;
                return;
            }
            // Check to see if we have dispatched ALL the messages in the buffer
            if(bytesLeft < 8) //badri     if bytesLeft is Less than 8
            {
                  ::memmove((void *)ptr, (const void *)&(ptr[iTotalLen]), sizeof(char) * bytesLeft);
                  return;
            }
        
            // Reset locals   (NER - no need... they're locals.
            cpyBuff = 0;
            msg = 0;
            
        } // end of while(1)
        
        // Free the Rx buffer
        free(ptr);
        
        #ifdef COMMOUT
             //Process the message
            try
            {
                if ( msg->m_messageID == MSG_SM_OC_DATABASE_PATH_INFO )
                {
                    WriteLog("OperatorCLientForm : Connected to server received - MSG_SM_OC_DATABASE_PATH_INFO");
                    LogonRequest(msg);
                }
                else
                {
                    ::PostThreadMessage(pRouterThread->ThreadID, msg->m_messageID, (WPARAM)msg,0);
                }
            }
            catch(...)
            {
                //ShowMessage("Server not located");
                ShowMessage( GetString(ID_SVR_NOT_LOC) );
                PSM_TransFilterForm->TransGridHeaderDisplay();
            }
        #endif
    }
    catch(...)
    {
        WriteLog(L_ERROR,"TPSM_OperatorClientForm :: csktClientSocketRead Failed ");
    }
}
/*EOS*/
