title: Creating 100,000 tcp connections using .NET
body: <p>I am writing a little Comet server in C#, and to test it I have written a little program that opens a bunch of connections, writes a little text to each of them, and then closes each of them:</p>
  
  <pre><code>int basePort = 30000;
  IPAddress localAddress = new IPAddress( new byte[] { 127, 0, 0, 1 } );
  List&lt;Socket&gt; sockets = new List&lt;Socket&gt;();
  
  for( int i = 0; i &lt; 20000; i++ ) {
      Socket s = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
      s.Bind( new IPEndPoint( localAddress, basePort + i ) );
      s.Connect( "localhost", 1999 );
      sockets.Add( s );
  }
  
  string message = "hello";
  byte[] messageData = Encoding.ASCII.GetBytes( message );
  foreach( Socket s in sockets ) {
      s.Send( messageData );
  }
  
  foreach( Socket s in sockets ) {
      s.Disconnect( false );
  }
  </code></pre>
  
  <p>I am using Windows XP at the moment, which only assigns dynamic client ports from the 1025 to 5000 range, so I have added explicit binding to ports starting at 30000.  This took me from under 4000 connections to a little more than 16000, but now I am getting the following exception on Socket.Connect:</p>
  
  <p>"An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full 127.0.0.1:1999"</p>
  
  <p>Any thoughts?  Changing the send and receive buffer size doesn't seem to make any difference, and it always seems to be my client app that breaks, never my server.  I realize I'm going to run out of client ports before I get to 100,000 connections, but I'd still like to understand what is going on a little better.</p>