title: Why Socket.AcceptAsync isn't firing SocketAsyncEventArgs Completed event?
body: <p>I'm developing a Server application which will receive messages and respond. Nothing really new. So, actually I'm following <a href="https://stackoverflow.com/questions/869744/how-to-write-a-scalable-tcp-ip-based-server">this answer</a> and <a href="http://www.codeproject.com/Articles/83102/C-SocketAsyncEventArgs-High-Performance-Socket-Cod" rel="nofollow noreferrer">this post</a>, but I can't get the <code>AcceptAsync()</code> method to fire the <code>Completed</code> event. Searched everywhere on internet, tried similar problem's solutions, but nothing seems to work for me.</p>
  
  <p>I've also tried to call <code>server.Start()</code> from a <code>Task.Run()</code>, but no luck. I know the server is listening fine because I can see it on <code>netstat -an</code>, I'm also able to connect using telnet if I break after <code>Listen()</code>.</p>
  
  <p>From what I've understood, if <code>AcceptAsync()</code> method returns true, it'll raise <code>SocketAsyncEventArgs.Completed</code> event which in turn will call again <code>StartAccept()</code> method and will loop until I force quit.</p>
  
  <p>The <code>SocketAsyncEventArgs.Completed</code> is right also: <a href="http://prntscr.com/8l3x8p" rel="nofollow noreferrer">http://prntscr.com/8l3x8p</a>, but still doesn't work.</p>
  
  <p>Here is my piece of code:</p>
  
  <pre><code>public class TTSServer
  {
      private Socket m_serverSocket;
      private IPEndPoint m_serverEndPoint;
  
      [DefaultValue(39454)]
      public int Port { get; set; }
      [DefaultValue(100)]
      public int IncommingQueueSize { get; set; }
      [DefaultValue(512)]
      public int BufferSize { get; set; }
  
      //Listeners to hold event when something happens.
  
      public static void Main(string[] args)
      {
          TTSServer server = new TTSServer(39454, 100, 512);
          server.Start();
      }
  
      public TTSServer(int port, int queueSize, int bufferSize)
      {
          Port = port;
          IncommingQueueSize = queueSize;
          BufferSize = bufferSize;
      }
  
  
      public void Start()
      {
          Console.WriteLine("Starting TTS Server (Port: {0}, QueueSize: {1}, BufferSize: {2})", Port, IncommingQueueSize, BufferSize);
          m_serverEndPoint = new IPEndPoint(IPAddress.Any, Port);
          m_serverSocket = new Socket(m_serverEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
          Console.WriteLine("Binding ({0})", m_serverEndPoint.ToString());
          m_serverSocket.Bind(m_serverEndPoint);
          Console.WriteLine("Listening");
          m_serverSocket.Listen(IncommingQueueSize);
  
          StartAccept(null);
      }
  
      public void Stop()
      {
  
      }
  
      /// &lt;summary&gt;
      /// Receive a incomming connection attemp in an asynchronous way.
      /// &lt;/summary&gt;
      /// &lt;param name="socketEvent"&gt;If is null, a new object is created, else, it'll be used for cache friendly reason.&lt;/param&gt;
      private void StartAccept(SocketAsyncEventArgs socketEvent)
      {
          //If received reference points to null, let's create a new object.
          if (socketEvent == null)
          {
              Console.WriteLine("Accepting new connections...");
              socketEvent = new SocketAsyncEventArgs();
              socketEvent.Completed += AcceptCompleted; //Add a callback on completed accept incomming connections attemp.
          }
          else
          {
              //Clear current incomming connection socket, so object may be reused.
              socketEvent.AcceptSocket = null;
          }
  
          //If there is an incomming connection pending(pooled), this method will receive the connection in a async way (witch returns true) 
          //and will call SocketAsyncEventArgs.Completed callback when done. 
          //Else, it waits for a new connection, returns false, and don't won't SocketAsyncEventArgs.Completed callback when done, so we have to 
          //call it manually.
          bool async = true;
  
          //When I debug this code, async receive true from AcceptAsync.
          async = m_serverSocket.AcceptAsync(socketEvent);
  
          if (!async)
          {
              AcceptCompleted(this, socketEvent);
          }
      }
  
      /// &lt;summary&gt;
      /// Handles a incomming connection after it's fully received. This function will do the business logic for incomming connections and prepare
      /// to receive data.
      /// &lt;/summary&gt;
      /// &lt;param name="sender"&gt;Object who posted this function&lt;/param&gt;
      /// &lt;param name="socketEvent"&gt;Information of the incomming connection&lt;/param&gt;
      private void AcceptCompleted(object sender, SocketAsyncEventArgs socketEvent)
      {
          Connection connection = new Connection(this, socketEvent.AcceptSocket, BufferSize);
  
          SocketAsyncEventArgs connectedSocketEvent = new SocketAsyncEventArgs();
          connectedSocketEvent.UserToken = connection;
  
          //Add a receive callback, to be called whenever the Receive function is finished.
          connectedSocketEvent.Completed += FlushBuffer;
          ReceiveData(connectedSocketEvent);
  
          //Accept next incomming connection.
          StartAccept(socketEvent);
      }
  </code></pre>
  
  <p>Don't know why, the <code>AcceptCompleted</code> is never fired, even when the <code>AcceptAsync()</code> method returns <code>true</code>.</p>