title: What is the async/await equivalent of a ThreadPool server?
body: <p>I am working on a tcp server that looks something like this using synchronous apis and the thread pool:</p>
  
  <pre><code>TcpListener listener;
  void Serve(){
    while(true){
      var client = listener.AcceptTcpClient();
      ThreadPool.QueueUserWorkItem(this.HandleConnection, client);
      //Or alternatively new Thread(HandleConnection).Start(client)
    }
  }
  </code></pre>
  
  <p>Assuming my goal is to handle as many concurrent connections as possible with the lowest resource usage, this seems that it will be quickly limited by the number of available threads. I suspect that by using Non-blocking Task apis, I will be able to handle much more with fewer resources.</p>
  
  <p>My initial impression is something like:</p>
  
  <pre><code>async Task Serve(){
    while(true){
      var client = await listener.AcceptTcpClientAsync();
      HandleConnectionAsync(client); //fire and forget?
    }
  }
  </code></pre>
  
  <p>But it strikes me that this could cause bottlenecks. Perhaps HandleConnectionAsync will take an unusually long time to hit the first await, and will stop the main accept loop from proceeding. Will this only use one thread ever, or will the runtime magically run things on multiple threads as it sees fit?</p>
  
  <p>Is there a way to combine these two approaches so that my server will use exactly the number of threads it needs for the number of actively running tasks, but so that it will not block threads unnecessarily on IO operations?</p>
  
  <p>Is there an idiomatic way to maximize throughput in a situation like this?</p>