title: Error in recieving data from TCP Client
body: <p>I've written an application which listens to a port and receives some packets,according to my customized protocol, the packets are either 49 byte to 1500 byte, which i can tell from data length in the packet. the way i should interpret and deal with data in 49 byte packets and bigger packets are different.</p>
  
  <p>The problem is that when i receive packets less than 1374 byte everything is ok, but when the packet length gets more, i receive the following exception and i also lose 4 last bytes of my data(i've tested with a 1384byte packet and i lost the last 4 bytes)</p>
  
  <p>Exception which is raised: 
     Index was out of range. Must be non-negative and less than the size of the collection.
     Parameter name: startIndex</p>
  
  <p>each 49 byte packet has 35 byte of data, and the data length of bigger packets are non-deterministic(because of compression). </p>
  
  <p>i found out sometimes the last 4 bytes are in a seperate "bytes" and "result" variables,meaning they are being treated like new packets and are not being attached to the packet they belong to.</p>
  
  <p>here's the code for receiving data:</p>
  
  <pre><code>TcpClient Client = obj as TcpClient;
          EndPoint ep = Client.Client.RemoteEndPoint;
          List&lt;Byte&gt; result = new List&lt;byte&gt;();
          result.Capacity = 2000;
          try
          {
  
  
              NetworkStream stream = Client.GetStream();
              int i = 49;
              while ((i = stream.Read(bytes, 0,49)) != 0)
              {
  
                  for (int id = 0; id &lt; i; id++)
                  {
                      result.Add(bytes[id]);
                  }
  //reading data length to determine packet length
                  byte[] tmp = new byte[2];
                  tmp = BitConverter.GetBytes(BitConverter.ToUInt16(result.ToArray(), 9));
                  if (BitConverter.IsLittleEndian)
                  {
                      Array.Reverse(tmp);
                  }
                  Int16 l = BitConverter.ToInt16(tmp, 0);
                  if (l&gt;35)
                  {
                      stream.Read(bytes, result.Count, l - 35);
                      for (int id = 49; id &lt;((l-35)+49); id++)
                      {
                          result.Add(bytes[id]);
                      }
                      if (this.TCPDataReceivedHandler != null)
                      {
                          this.TCPDataReceivedHandler(ep, result.Count, result.ToArray());
                          result.Clear();
                          Array.Clear(bytes, 0, 2000);
                          result.Capacity = 2000;
                      }
                  }
                  else
                  {
                      if (this.TCPDataReceivedHandler != null)
                      {
                          this.TCPDataReceivedHandler(ep, result.Count, result.ToArray());
                          result.Clear();
                          Array.Clear(bytes, 0, 2000);
                          result.Capacity = 2000;
                      }
                  }
  
  
              }
              System.Diagnostics.Debug.WriteLine("client Close");
              Client.Close();
  
          }
          catch (System.Exception ex)
          {
              throw ex;
          }
          finally
          {
              Client.Close();
              this.clients.Remove(Client);
          }
  </code></pre>
  
  <p>According to Greg Suggestion and my researches,i also tried using following method:</p>
  
  <pre><code>NetworkStream stream = Client.GetStream();
              int bytesread = 0, OffsetTemp = 0;
  
              while (stream.CanRead)
              {
                  OffsetTemp = 0;
  
                  bytesread += stream.Read(bytess, OffsetTemp, 11);
                  OffsetTemp = OffsetTemp + 11;
  
  
  
                  byte[] tmp = new byte[2];
                  tmp = BitConverter.GetBytes(BitConverter.ToUInt16(bytess.ToArray(), 9));
                  if (BitConverter.IsLittleEndian)
                  {
                      Array.Reverse(tmp);
                  }
                  Int16 l = BitConverter.ToInt16(tmp, 0);
  
                  bytesread += stream.Read(bytess, OffsetTemp++, 11 + l + 3);
                  for (int id = 0; id &lt; l + 14; id++)
                  {
                      result.Add(bytess[id]);
                  }
  
                  if (this.TCPDataReceivedHandler != null)
                  {
                      this.TCPDataReceivedHandler(ep, result.Count, result.ToArray());
                      result.Clear();
                      Array.Clear(bytess, 0, 2000);
                  }
  
              }
  </code></pre>