title: Stack overflow when using the System.Net.Sockets.Socket.AcceptAsync model
body: <p>With respect to C# and .NET's <a href="http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.acceptasync.aspx" rel="noreferrer"><code>System.Net.Sockets.Socket.AcceptAsync</code></a> method, one would be required to handle a return value of "false" in order to handle the immediately available <code>SocketAsyncEventArgs</code> state from the synchronously processed connection. Microsoft provides examples (found on the <code>System.Net.Sockets.SocketAsyncEventArgs</code> class page) which will cause a stack overflow if there are a large amount of pending connections, which can be exploited on any system that implements their handling model. </p>
  
  <p>Other ideas for getting around this issue are to make a loop that calls the handler method, with the condition being that the value <code>Socket.AcceptAsync</code> returns is equal to false, and to break the loop (to allow deferred processing) if the value is indicating that the operation is being completed asynchronously (true). However, this solution also causes a stack overflow vulnerability because of the fact that the callback associated with the <code>SocketAsyncEventArgs</code> passed to <code>Socket.AcceptAsync</code> has at the end of the method, a call to <code>Socket.AcceptAsync</code>, which also has a loop for immediately available, synchronously accepted, connections. </p>
  
  <p>As you can see, this is a pretty solid problem, and I've yet to find a good solution that does not involve <code>System.Threading.ThreadPool</code> and creating tons of other methods and scheduling processing. As far as I can see, the asynchronous socket model relating to <code>Socket.AcceptAsync</code> requires more than what is demonstrated in the examples on MSDN.</p>
  
  <p>Does anyone have a clean and efficient solution to handling immediately pending connections that are accepted synchronously from Socket.AcceptAsync without going into creating separate threads to handle the connections and without utilizing recursion?</p>