title: C# Server - TCP/IP Socket Efficiency
body: <p>Good day all!</p>
  <p>I am working on a open source server for a game that is closed source - the game operates using TCP/IP sockets (instead of UDP, doh...) So being a connection based protocal, I am contrained to work with this.</p>
  <p>My current program structure (dumbed down):</p>
  <blockquote>
  <p>Core Thread</p>
  <ul>
  <li>Receive new connection and create a new client object.</li>
  </ul>
  </blockquote>
  <hr />
  <blockquote>
  <p>Client Object</p>
  <ul>
  <li><p>IOloop (runs on its own thread)</p>
  <ul>
  <li>Get data from socket, process packets. (one packet at a time)</li>
  <li>Send data buffered from other threads (one packet at a time)</li>
  </ul>
  </li>
  </ul>
  </blockquote>
  <p>The client will send data immediately (no delay) when it is it's own thread.</p>
  <hr />
  <p>I am noticing a huge flaw with the program, mainly it sends data very slow. Because I send data packet by packet, synchronously. (Socket.Send(byte[] buffer))</p>
  <p>I would like to send data almost immediately, without delay - asynchronously.</p>
  <p>I have tried creating a new thread every time I wanted to send a packet (so each packet sends on it's own managed thread) but this was a huge mess.</p>
  <p>My current system uses a synchronous sending with nagle algorithm disabled - but this has the flaw of bottlenecking - send one packet, send operation blocks until TCP to confirms, then send the next... I can issue easily 10 packets every 100ms, and if the packets take 400ms to send, this backs up and breaks. Of course on my Local host I don't get this issue.</p>
  <hr />
  <p>So, my question: What would be the best way to send multiple small packets of data? I am thinking of merging the data to send at the end of every IO thread loop to one large byte buffer, to reduce the back and forth delay - but the obvious problem here is this undermines the nagle algorithm avoidance which I had hoped would stop the delays.</p>
  <p>How does a synchronous send work? Does it block until the data is confirmed correctly received by the recipient as I believe? Is there a way I can do this without waiting for confirmation? I do understand the packets must all go in order and correctly (as per the protocol specification).</p>