/// <summary> /// Method to be called when data has been received /// </summary> /// <param name="ar"></param> private static void ReceiveCallback(IAsyncResult ar) { SocketState ss = (SocketState)ar.AsyncState; int bytesRead = 0; try { //A client may be disconnected bytesRead = ss.theSocket.EndReceive(ar); } catch (SocketException se) { ss.DisconnectCallback(ss); } // If the socket is still open if (bytesRead > 0) { string theMessage = Encoding.UTF8.GetString(ss.messageBuffer, 0, bytesRead); // Append the received data to the growable buffer. // It may be an incomplete message, so we need to start building it up piece by piece ss.sb.Append(theMessage); ss.CallMe(ss); } }