title: NullReferenceException in TCPClient when connecting to the server in a loop
body: <p>I am testing some tcp ip based server by my test client under .net in c#.</p>
  
  <p>My test client class is some trivial async socket code:</p>
  
  <pre><code>public void Connect()
      {
          try
          {
              _tcpClient.BeginConnect(IPAddress, Port, OnConnectCallBack, null);
          }
          catch (Exception exc)
          {
              Debug.Assert(false, exc.Message);
          }
      }
  
  private void OnConnectCallBack(IAsyncResult ar)
      {
          try
          {
              _tcpClient.EndConnect(ar);
  
          }
          catch (Exception exc)
          {
              Debug.WriteLine("Connection error: " + exc.Message);
          }
      }
  </code></pre>
  
  <p>_tcpClient is a TcpClient instance.</p>
  
  <p>I try to make several (100) connections to the server at a time to stress test it. If I add some 50 msec sleep between calling connect, it works like a charm. But, when I try to connect in a foreach(...) iteration without adding sleep between connections, OnConnectCallBack throws the exception: <em>A first chance exception of type 'System.NullReferenceException' occurred in System.dll. Connection error: Object reference not set to an instance of an object.</em> Sometimes I get <em>target machine refuse the connection</em> exception.</p>
  
  <p>After setting a breakpoint, I noticed it comes from _tcpClient: none of its methods and property values can be seen, all of them has null reference exception value, and its Client property is null.</p>
  
  <p>at TestClient.OnConnectCallBack(IAsyncResult ar)  C:...\TestClient.cs(82)
      at LazyAsyncResult.Complete(IntPtr userToken)<br>
      at ContextAwareResult.CompleteCallback(Object state)<br>
      at ExecutionContext.runTryCode(Object userData)<br>
      at RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)<br>
      at ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)<br>
      at ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)<br>
      at ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)<br>
      at ContextAwareResult.Complete(IntPtr userToken)<br>
      at LazyAsyncResult.ProtectedInvokeCallback(Object result, IntPtr userToken)<br>
      at LazyAsyncResult.InvokeCallback(Object result)<br>
      at QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()<br>
      at ThreadPoolWorkQueue.Dispatch()<br>
      at _ThreadPoolWaitCallback.PerformWaitCallback()</p>