title: C# Concurrent and Async Socket Connections
body: <p>I'm working on a simple socket server that is supposed to take in multiple connections and hand them off to a "connection" class by means of <code>EndAccept()</code> The problem is that after accepting a connection, the code doesn't accept anything further until the connection ends.</p>
  
  <p>I create the socket in <code>Main()</code> like so, then I pass the socket to ServerEnvironment (static class) via <code>Initialize()</code>.</p>
  
  <pre><code>MainSocket.Bind(new IPEndPoint(IPAddress.Parse(Addr), Port));
  MainSocket.Listen(10);
  
  ServerEnvironment.Initialize(MainSocket);
  
  while (true)
  {
      Console.ReadLine();
      Console.WriteLine("&gt;&gt;");
  }
  </code></pre>
  
  <p>Once <code>MainSocket</code> has been passed, ServerEnvironment takes over from there.</p>
  
  <pre><code>static Socket MainSocket;
  
  public static void Initialize(Socket _MainSocket)
  {
      MainSocket = _MainSocket;
  
      AcceptGameConnection = new AsyncCallback(AddConnection);
      MainSocket.BeginAccept(AcceptGameConnection, null);
  }
  
  public static void AddConnection(IAsyncResult Result)
  {
      Socket UserSocket = MainSocket.EndAccept(Result);
      ConnectionCount++;
  
      // Removed the stuff that happens to UserSocket because the same
      // symptoms occur regardless of their presence.
  
      MainSocket.BeginAccept(AcceptGameConnection, null);
   }
  </code></pre>
  
  <p>As I search about this question, I come to find that multithreading could be a necessity for my purposes. However, when I use <code>Console.WriteLine(Thread.CurrentThread.ManagedThreadId);</code> in both <code>Initialize();</code> and <code>AddConnection();</code>, two different thread IDs appear, so I assume that multithreading is already a capability and I don't need to manually create a thread. That wouldn't explain my issue, though.</p>
  
  <p>Is multithreading necessary for me to be able to have concurrent and asynchronous socket connectivity?</p>
  
  <p>EDIT:
  Binding error..was binding to my LAN address which caused some problems.</p>