title: Merge splitted bytes from socket for further usage?
body: <p>I have a TCP socket application where I have to read several types of replies being the max size of a buffer 8192 some replies are splitted in more packets.</p>
  
  <p>Currently I receive a list of members at reply 44, so the first idea I had to be able to deal with splitted packets was to define a stream out side of it to stored the incoming data until it is complete with a bool and current size variable.</p>
  
  <p>Once it hits the reply 44 it will check if extraList is true or false, if false it means it is an initial request to incoming members list.</p>
  
  <p>If the 4 initial bytes of the packet is bigger then bytes.Legth which is 8192 it will trigger the extraList to true and fill the initial data to the buffer I had previously set with the with the total packet size as it is size.</p>
  
  <p>Since extraList has been trigged and turned into true, the packet reading will fall into it until the data is complete, which will then set it back to false and trigger the MemberList function with the complete list.</p>
  
  <p>Would like some advices, suggestions, etc to improve this code.</p>
  
  <pre><code>int storedCurSize = 0;
  MemoryStream stored = null;
  bool extraList = false;
  
  while (roomSocket.Connected)
  {
      byte[] bytes = new byte[roomSocket.ReceiveBufferSize];
      roomSocket.Receive(bytes);
  
      MemoryStream bufferReceived = new MemoryStream(bytes, 0, bytes.Length);
      using (var reader = new BinaryReader(bufferReceived))
      {
          int packetSize = (int)reader.ReadInt32() + 9;
          int reply = (int)reader.ReadByte();
          if (reply == 44 || extraList)
          {
              if (!extraList &amp;&amp; packetSize &lt;= bytes.Length)
              {
                  MemberList(bytes);
              }
              else
              {
                  if (!extraList)
                  {
                      stored = new MemoryStream(new byte[packetSize], 0, packetSize);
                      stored.Write(bytes, 0, bytes.Length);
                      storedCurSize = bytes.Length;
                      extraList = true;
                  }
                  else
                  {
                      if (storedCurSize &lt; stored.Length)
                      {
                          int storedLeftSize = (int)stored.Length - storedCurSize;
                          stored.Write(bytes, 0, (storedLeftSzie &lt; bytes.Length) ? storedLeftSize : bytes.Length);
                          storedCurSize += (storedLeftSize &lt; bytes.Length) ? storedLeftSize : bytes.Length;
                          if (storedCurSize &gt;= stored.Length)
                          {
                              extraList = false;
                              MemberList(stored.ToArray());
                              stored.Close();
                          }
                      }
                  }
              }
          }
      }
  }
  </code></pre>