title: Socket EndReceive Order / Data Issues
body: <p><strong>Background:</strong></p>
  
  <p>The application I am programming uses async sockets (using BeginSend, EndSend, BeginReceive, EndReceive) to send data between each other. The sockets are TCP, no socket flags, on IPV4.</p>
  
  <p>It uses the system where it sends a 4-byte (int) message, followed by a message with the length specified in the previous message. I use function helpers that handle the MessageLength, and the MessageBody. The flow is something like this</p>
  
  <ol>
  <li>BeginReceive() </li>
  <li>EndReceive()</li>
  <li>MessageLengthReceived()</li>
  <li>BeginReceive()</li>
  <li>MessageBodyReceived()</li>
  </ol>
  
  <p><strong>Issue:</strong></p>
  
  <p>The issue arrives when I send file data, in chunks of 16kb (with an additional small overhead: offset, pieceIndex, etc). Occasionally, when receiving the MessageLength, it receives a data from a <em>random part</em> in the previous message, instead of the actual message length. Part of this issue is that it doesn't always happen at a set offset (eg beginning or end of file / piece / 16 kb chunk) and can happen with any file, but happens more if I send a lot more files / larger files.</p>
  
  <p>There are internal messages that are sent (eg RequestMessages) that never experience this problem. All the internal messages are &lt; 100 bytes.</p>
  
  <p>I've tried waiting for the file chunk to save completely before requesting another chunk, but it still fails. I've also tried limiting how many chunks to send at a time, but this only resolves the issue when using 127.0.0.1 (local clients), and not cross network (LAN).</p>
  
  <p>I've spent hours going through my application to see if there's any issues, but I have yet to see any where it would be sending the wrong data as a header. The issue always seems to inbetween the send and the receive of the two clients. Is there settings for socket / method of sending that I should be using? Or could it be some sort of race condition (I thought about race condition, but the fact that the data can be anywhere randomly in a file made me rethink this).</p>