title: Instead of scanning multiple ports I'm scanning only one and when port is closed my app closes
body: <p>I'm trying to scan multiple ports at once using asynchronymous scanning. The problem is that I can only display the first working port and then waiting like 20 seconds my app is closing with out telling me that the port is closed.</p>
  
  <p>What could be wrong with this code?</p>
  
  <pre><code>private void btnStart_Click(object sender, EventArgs e)
  {
      for (int port = 80; port &lt; 100; port++)
      {
          ScanPort(port);
      }
  }
  
  private void ScanPort(int port)
  {
      var client = new TcpClient();
  
      try 
      {
          client.BeginConnect(IPAddress.Parse("74.125.226.84"), port, new AsyncCallback(CallBack), client);
      } 
      catch (SocketException) 
      {
          client.Close();
      }
  }
  
  private void CallBack(IAsyncResult result)
  {
      var client = (TcpClient)result.AsyncState;
  
      client.EndConnect(result);
  
      if (client.Connected)
      {
          this.Invoke((MethodInvoker)delegate
          {
              txtDisplay.Text += "open2" + Environment.NewLine;
          });
      }
      else
      {
          this.Invoke((MethodInvoker)delegate
          {
              txtDisplay.Text += "closed2" + Environment.NewLine;
          });
      }
  }
  </code></pre>