title: c# server socket data not receiving properly
body: <p>i have developed one application which will interact with the plc using TCP server socket program.
  each cycle plc is sending one signal to my application as character('R').<br>
  but some times i am not receiving any character. that means event is not firing.
  but next time when plc sends 'R', i will receive it as 'RR'.
  my code is `</p>
  
  <pre><code>private void _tcpServerFortest_OnRead(Socket soc)
      {
        //  rec = new byte[1];
          byte[] rec = _tcpServerFortest.ReceivedBytes;
  
          string str = System.Text.ASCIIEncoding.ASCII.GetString(rec);`
  }
  </code></pre>
  
  <p>and my code for starting TCP server is </p>
  
  <pre><code>   _tcpServerFortest = new CServerSocket(2005);
                  _tcpServerFortest .OnConnect += _tcpServerFortest _OnConnect;
                  _tcpServerFortest .OnDisconnect += _tcpServerFortest _OnDisconnect;
                 _tcpServerFortest .OnRead += _tcpServerFortest _OnRead;
                 _tcpServerFortest .Active();
  </code></pre>
  
  <p>in my class cServerSocket, data received method is as below</p>
  
  <pre><code> private void OnDataReceived(IAsyncResult asyn)
      {
          SocketPacket socketData = (SocketPacket)asyn.AsyncState;
          try
          {
              int iRx = socketData.m_currentSocket.EndReceive(asyn);
              if (iRx &lt; 1)
              {
                  socketData.m_currentSocket.Close();
                  if (!socketData.m_currentSocket.Connected)
                  {
                      if (OnDisconnect != null)
                          OnDisconnect(socketData.m_currentSocket);
                      Clients.Remove(socketData.m_currentSocket);
                      socketData.m_currentSocket = null;
                  }
              }
              else
              {
  
                  mBytesReceived = socketData.dataBuffer;
                  char[] chars = new char[iRx + 1];
                  Decoder d = Encoding.UTF8.GetDecoder();
                  d.GetChars(socketData.dataBuffer, 0, iRx, chars, 0);
                  mTextReceived = new String(chars);
                  if (OnRead != null)
                      OnRead(socketData.m_currentSocket);
                  WaitForData(socketData.m_currentSocket);
              }
          }
          catch (InvalidOperationException ex)
          {
              if (socketData.m_currentSocket.Connected)
                  socketData.m_currentSocket.Close();
              if (!socketData.m_currentSocket.Connected)
              {
                  if (OnDisconnect != null)
                      OnDisconnect(socketData.m_currentSocket);
                  Clients.Remove(socketData.m_currentSocket);
                  socketData.m_currentSocket = null;
              }
              else
                  if (OnError != null)
                      OnError(ex.Message, null, 0);
          }
          catch (SocketException se)
          {
              if (OnError != null)
                  OnError(se.Message, socketData.m_currentSocket, se.ErrorCode);
              if (!socketData.m_currentSocket.Connected)
              {
                  if (OnDisconnect != null)
                      OnDisconnect(socketData.m_currentSocket);
                  Clients.Remove(socketData.m_currentSocket);
                  socketData.m_currentSocket = null;
              }
          }
      }
  </code></pre>
  
  <p>can anyone help me out to solve this problem?</p>