Socket Listener Multiple Simultaneous AcceptAsync Calls
<p>So I've been looking at socket programming and reading up on ways to create high performance servers.</p>

<p>Reading about using the <code>AcceptAsync</code> method of <code>Socket</code> and how sometimes the calls may execute syncronously (and return false) if connections are immediately available, and how to put the socket back into a listening state after accepting a connection, I started to wonder if that meant that really, although a socket accepting connections using <code>AcceptAsync</code> would "wait" asynchronously, ultimately it would only process 1 new connection at a time, even if 1000 clients connected at once, and depending on how quickly clients could be passed off to another thread for processing, may cap the performance.</p>

<p>This then lead me to wonder if I could even write my class with this in mind, using a single instance of <code>SocketAsyncEventArgs</code> for all connections, rather than a object pool that I had originally intended.</p>

<p>Thinking more about this, I wondered in that case what would happen if multiple <code>AcceptAsync</code> calls were made while no connections were available. So I made this very very rough test application to see what would happen:</p>

<pre><code>class Program
{
    private static Socket s;

    static void Main(string[] args)
    {
        s = new Socket(IPAddress.Any.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
        s.Bind(new IPEndPoint(IPAddress.Any, 10001));
        s.Listen(10);
        var e = new SocketAsyncEventArgs();
        e.Completed += EOnCompleted;
        s.AcceptAsync(e);
        e = new SocketAsyncEventArgs();
        e.Completed += EOnCompleted;
        s.AcceptAsync(e);
        e = new SocketAsyncEventArgs();
        e.Completed += EOnCompleted;
        s.AcceptAsync(e);
        e = new SocketAsyncEventArgs();
        e.Completed += EOnCompleted;
        s.AcceptAsync(e);
        while (true)
        {

        }
    }

    private static void EOnCompleted(object sender, SocketAsyncEventArgs socketAsyncEventArgs)
    {
        Console.WriteLine(socketAsyncEventArgs.AcceptSocket.RemoteEndPoint);
        Thread.Sleep(10000);
        var e = new SocketAsyncEventArgs();
        e.Completed += EOnCompleted;
        s.AcceptAsync(e);
    }
}
</code></pre>

<p>I used a tcp testing application to spam the application with connections and the result showed that the multiple <code>AcceptAsync</code> calls created multiple Accept/Complete cycles that seemed to execute in parallel.</p>

<p>My question is, does this actually have any benefit? Is it a legitimate way to allow faster processing of incoming connections? Are there any drawbacks?</p>

<p>EDIT 1: Looking into this further it seems like it's technically possible that if data is always available for processing for async calls that they'll all run synchronously and never return to accept a new connection, or at least, there could be a significant delay.</p>

<p>How can you produce a high performance server if this is indeed the case?</p>