title: Multithreading with SSH.Net: bottleneck issue
body: <p>I'm currently working on a <strong>C#</strong> app that connects to different remote nodes via <strong>SSH using SSH.Net</strong> library, and then executes a list of commands on said nodes and returns the output.</p>
  
  <p>I need to be able to connect to many nodes at the same time, but it's considered normal to not be able to connect to some of the nodes in the given list, in which case I supply a timeout value.</p>
  
  <p>Now, this is a <strong>multithreaded</strong> application. <strong>Each thread will connect to its own node</strong>. Here's an example of the code executed in each thread:</p>
  
  <pre><code>Stopwatch sw = new Stopwatch();
  try{
  
      KeyboardInteractiveAuthenticationMethod kauth = new KeyboardInteractiveAuthenticationMethod(Username);
      PasswordAuthenticationMethod pauth = new PasswordAuthenticationMethod(Username, Password);
  
      kauth.AuthenticationPrompt += (sender, e) =&gt; HandleKeyEvent(sender, e, Password);
  
      ConnectionInfo connectionInfo = new ConnectionInfo(IPAddress, Username, pauth, kauth);
      connectionInfo.Timeout = TimeSpan.FromSeconds(5);
      client = new SshClient(connectionInfo);
      Console.WriteLine("Trying to connect to " + IPAddress);
      sw.Start();
      client.Connect();
  
  } catch (Exception e) {
      ...
  } finally {
      sw.Stop();
  }
  //Here I'm expecting a failure for each node.
  Console.WriteLine("Failed to connect to " + IPAddress + ". Elapsed: " + sw.Elapsed.TotalSeconds  );
  </code></pre>
  
  <p>Here is my issue: I face a bottleneck in the amount of thread that can use the '.Connect()' SSH.Net method at the same time. Only 3 threads can execute <code>client.Connect();</code>, while the others are blocked. The output I get will look something like this for 10 nodes:</p>
  
  <pre><code>Trying to connect to IP_1
  Trying to connect to IP_2
  Trying to connect to IP_3
  Trying to connect to IP_4
  Trying to connect to IP_5
  Trying to connect to IP_6
  Trying to connect to IP_7
  Trying to connect to IP_8
  Trying to connect to IP_9
  Trying to connect to IP_10
  Failed to connect to IP_1. Elapsed: 5
  Failed to connect to IP_2. Elapsed: 5
  Failed to connect to IP_3. Elapsed: 5
  Failed to connect to IP_4. Elapsed: 10
  Failed to connect to IP_5. Elapsed: 10
  Failed to connect to IP_6. Elapsed: 10
  Failed to connect to IP_7. Elapsed: 15
  Failed to connect to IP_8. Elapsed: 15
  Failed to connect to IP_9. Elapsed: 15
  Failed to connect to IP_10. Elapsed: 20
  </code></pre>
  
  <p>Does anybody have an idea of what is causing this and how I could resolve this bottleneck? </p>
  
  <p>Why am I limited to 3 'connect' operations at once? </p>
  
  <p>I'm using a 5 seconds timeout for development, but the final version will likely have a timeout of 30sec/1minute and will try to connect to ~100 nodes, which will greatly reduce the speed of the app when encountering a few that can't be reached.</p>