title: Tips / techniques for high-performance C# server sockets
body: <p>I have a .NET 2.0 server that seems to be running into scaling problems, probably due to poor design of the socket-handling code, and I am looking for guidance on how I might redesign it to improve performance.</p>
  
  <p><strong>Usage scenario:</strong> 50 - 150 clients, high rate (up to 100s / second) of small messages (10s of bytes each) to / from each client. Client connections are long-lived - typically hours. (The server is part of a trading system. The client messages are aggregated into groups to send to an exchange over a smaller number of 'outbound' socket connections, and acknowledgment messages are sent back to the clients as each group is processed by the exchange.) OS is Windows Server 2003, hardware is 2 x 4-core X5355. </p>
  
  <p><strong>Current client socket design:</strong> A <code>TcpListener</code> spawns a thread to read each client socket as clients connect. The threads block on <code>Socket.Receive</code>, parsing incoming messages and inserting them into a set of queues for processing by the core server logic. Acknowledgment messages are sent back out over the client sockets using async <code>Socket.BeginSend</code> calls from the threads that talk to the exchange side.</p>
  
  <p><strong>Observed problems:</strong> As the client count has grown (now 60-70), we have started to see intermittent delays of up to 100s of milliseconds while sending and receiving data to/from the clients. (We log timestamps for each acknowledgment message, and we can see occasional long gaps in the timestamp sequence for bunches of acks from the same group that normally go out in a few ms total.) </p>
  
  <p>Overall system CPU usage is low (&lt; 10%), there is plenty of free RAM, and the core logic and the outbound (exchange-facing) side are performing fine, so the problem seems to be isolated to the client-facing socket code. There is ample network bandwidth between the server and clients (gigabit LAN), and we have ruled out network or hardware-layer problems.</p>
  
  <p>Any suggestions or pointers to useful resources would be greatly appreciated. If anyone has any diagnostic or debugging tips for figuring out exactly what is going wrong, those would be great as well.</p>
  
  <p><em>Note: I have the MSDN Magazine article <a href="http://msdn2.microsoft.com/en-us/magazine/cc300760.aspx" rel="noreferrer">Winsock: Get Closer to the Wire with High-Performance Sockets in .NET</a>, and I have glanced at the Kodart "XF.Server" component - it looks sketchy at best.</em></p>