public Object[] ReceiveData()
 {
     try
     {
         Monitor.Enter(this);
         m_socketClient.Blocking = false;
         long   nStart  = DateTime.Now.Ticks;
         int    nRead   = 0;
         int    nTotal  = 4;
         Byte[] pData   = new Byte[4];
         Byte[] pHeader = null;
         while (true)
         {
             try
             {
                 Thread.Sleep(m_nClientPause);
                 if (m_socketClient.Available > 0)
                 {
                     nRead += m_socketClient.Receive(pData, nRead, nTotal - nRead, SocketFlags.None);
                     if ((pData[0] & 0x0000000F) == 2)
                     {
                         nRead = 0;
                     }
                 }
             }
             catch (Exception) {}
             if (pHeader == null && nRead == 4)
             {
                 nTotal = (pData[1] & 0x000000FF) + (pData[2] & 0x000000FF) * 256 + (pData[3] & 0x000000FF) * 65536 + ((pData[0] & 0x000000FF) / 16) * 16777216;
                 if ((pData[0] & 0x0000000F) > 1)
                 {
                     throw new Exception("Invalid input data type byte");
                 }
                 if (nTotal > m_nMaxDataSize)
                 {
                     throw new Exception("Data size too large");
                 }
                 pHeader = pData;
                 nRead   = 0;
                 pData   = new Byte[nTotal];
             }
             if (pHeader != null && nRead == nTotal)
             {
                 break;
             }
             if (((DateTime.Now.Ticks - nStart) / 10000) > m_nReadTimeout * 1000)
             {
                 throw new Exception("Timeout while receiving incoming data");
             }
         }
         if ((pHeader[0] & 0x0000000F) == 1)
         {
             return(new Object[2] {
                 pData, null
             });
         }
         else
         {
             if (pData.Length % 2 != 0)
             {
                 throw new Exception("Invalid string data size");
             }
             return(new Object[2] {
                 null, XYNetCommon.BinaryToString(pData)
             });
         }
     }
     catch (Exception oBug)
     {
         Connect();
         m_exception = oBug;
         return(null);
     }
     finally
     {
         m_socketClient.Blocking = true;
         Monitor.Exit(this);
     }
 }
 private void ProcessInput(Socket sock, IPEndPoint ipe)
 {
     try
     {
         Byte[] pHeader = new Byte[4];
         int    nPos    = 0;
         long   nStart  = DateTime.Now.Ticks;
         while (nPos < 4)
         {
             if (sock.Available > 0)
             {
                 nPos += sock.Receive(pHeader, nPos, Math.Min(sock.Available, (4 - nPos)), SocketFlags.None);
                 if ((pHeader[0] & 0x000000FF) == 255)
                 {
                     sock.Shutdown(SocketShutdown.Both);
                     sock.Close();
                     Monitor.Enter(this);
                     m_htSockets.Remove(ipe.Address.ToString() + ":" + ipe.Port.ToString());
                     Monitor.Exit(this);
                     return;
                 }
             }
             else
             {
                 Thread.Sleep(m_nServerPause);
             }
             if (nPos < 4 && ((DateTime.Now.Ticks - nStart) / 10000) > m_nReadTimeout * 1000)
             {
                 throw new Exception("Timeout while receiving incoming data");
             }
         }
         if ((pHeader[0] & 0x0000000F) != 2)
         {
             int nSize = pHeader[1] + pHeader[2] * 256 + pHeader[3] * 65536 + (pHeader[0] / 16) * 16777216;
             if (nSize > m_nMaxDataSize)
             {
                 throw new Exception("Data size too large");
             }
             Byte[] pData = new Byte[nSize];
             nPos   = 0;
             nStart = DateTime.Now.Ticks;
             while (nPos < nSize)
             {
                 if (sock.Available > 0)
                 {
                     nPos += sock.Receive(pData, nPos, Math.Min(sock.Available, (nSize - nPos)), SocketFlags.None);
                 }
                 else
                 {
                     Thread.Sleep(m_nServerPause);
                 }
                 if (nPos < nSize && ((DateTime.Now.Ticks - nStart) / 10000) > m_nReadTimeout * 1000)
                 {
                     throw new Exception("Timeout while receiving incoming data");
                 }
             }
             Monitor.Enter(this);
             m_listSockets.Add(sock);
             Monitor.Exit(this);
             if ((pHeader[0] & 0x0000000F) == 1)
             {
                 if (m_delegateBinaryInputHandler != null)
                 {
                     m_threadPool.InsertWorkItem("Handle Binary Input", new BinaryInputHandlerDelegate(m_delegateBinaryInputHandler), new Object[3] {
                         ipe.Address.ToString(), ipe.Port, pData
                     }, false);
                 }
                 else
                 {
                     throw new Exception("No binary input handler");
                 }
             }
             else if ((pHeader[0] & 0x0000000F) == 0)
             {
                 if (m_delegateStringInputHandler != null)
                 {
                     m_threadPool.InsertWorkItem("Handle String Input", new StringInputHandlerDelegate(m_delegateStringInputHandler), new Object[3] {
                         ipe.Address.ToString(), ipe.Port, XYNetCommon.BinaryToString(pData)
                     }, false);
                 }
                 else
                 {
                     throw new Exception("No string input handler");
                 }
             }
             else
             {
                 throw new Exception("Invalid string data size");
             }
         }
     }
     catch (Exception oBug)
     {
         Monitor.Enter(this);
         m_htSockets.Remove(ipe.Address.ToString() + ":" + ipe.Port.ToString());
         Monitor.Exit(this);
         try
         {
             sock.Shutdown(SocketShutdown.Both);
             sock.Close();
         }
         catch (Exception) {}
         if (m_delegateExceptionHandler != null)
         {
             m_threadPool.InsertWorkItem("Handle Exception", m_delegateExceptionHandler, new Object[1] {
                 oBug
             }, false);
         }
         else
         {
             Monitor.Enter(this);
             m_exception = oBug;
             Monitor.Exit(this);
         }
     }
 }