title: .NET - Socket server with async/await
body: <p>I'm writing simple socket server with C#. The idea is to do it as simple as possible since the communication won't be heavy. I've used some of the TAP/APM patterns on socket async calls so the code now looks like this:</p>
  
  <pre><code>    public async Task StartListening()
      {
          try
          {
              var endpoint = new IPEndPoint(IPAddress.Loopback, Port);
  
              using (Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
              {
                  Socket.Bind(endpoint);
                  Socket.Listen(Limit);
                  while (!Shutdown)
                  {
                      // await till we get the connection - let the main thread/caller continue (I expect this loop to continue in separate thread after await)
                      var socket = await Socket.AcceptAsync().ConfigureAwait(false);
  
                      var state= new ClientStateObject(socket, id);
                      // do not await for receive - continue to listen for connections
                      Receive(socket, state);
                  }
              }
          }
          catch (Exception ex)
          {
              // handle errors via callbacks
          }
      }
  
      private async void Receive(Socket socket, ClientStateObject state)
      {
          try
          {
              while (!Shutdown)
              {
                  var bytes = await socket.ReceiveAsync(state).ConfigureAwait(false);
  
                  var readResult = state.Read(bytes);
                  if (readResult == CloseConn)
                  {
                      // close connection
  
                      return;
                  }
  
                  if (readResult == Completed)
                  {
                      // process the message
  
                      state.Reset();
                  }
              }
          }
          catch (Exception ex)
          {
              // handle errors via callbacks
          }
      }
  </code></pre>
  
  <p>This code seems to run fine in development builds, but sometimes behaves strange in release mode. I assume this might be related to race conditions or something that is related to threading and async/await. Does anyone see what might be the problem with the above code?</p>
  
  <p>Just to be clear AcceptAsync/ReceiveAsync are wrappers around socket methods to return tasks.</p>