title: How to change timeout of BeginConnect
body: <p>Consider the following program:</p>
  
  <pre><code>using System;
  using System.Diagnostics;
  using System.Net.Sockets;
  
  namespace ConsoleApplication1
  {
    class Program
    {
      static void Main(string[] args)
      {
        Socket sock = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
  
        try
        {
          Stopwatch watch = new Stopwatch();
          watch.Start();
  
          IAsyncResult res = sock.BeginConnect("localhost", 7000, null, null);
  
          res.AsyncWaitHandle.WaitOne( 5000 ); // 5 sec timeout
  
          watch.Stop();
          Console.WriteLine("Elapsed ms: " + watch.ElapsedMilliseconds);
  
          if (!sock.Connected) {
            sock.Close();
            Console.WriteLine("Failed to connect server.");
          }
        }
        catch (System.Exception ex) {
          Console.WriteLine(ex.Message);
        }
      }
    }
  }
  </code></pre>
  
  <p>Console Output:</p>
  
  <pre><code>Elapsed ms: 1014
  Failed to connect server.
  </code></pre>
  
  <p>I would have expected for <code>BeginConnect</code> to try to establish the connection for about 5 seconds, whereas the wait always returns after just 1 second.
  Why?</p>