/// <summary> /// This is a helper method to receive bytes from the server. /// </summary> /// <param name="message"></param> private void ReceiveNextMessage() { // should this be a while loop? while (receiveQueue.Count > 0) // if there are pending message requests { if (receivedMessages.Count > 0) // and if we have a message to send { ReceiveMessage justReceived = receiveQueue.Dequeue(); // get the request at top of queue string message = receivedMessages.Dequeue(); // get the message at top of queue // send the ReceiveMessage callback back to the client on a new thread ThreadPool.QueueUserWorkItem(o => justReceived.callback(message, null, justReceived.payload)); } else { break; } } // counter for keeping track of how many bytes have been received int bytesReceived = 0; // use the socket data structure to begin receiving the byte from the server if (receiveQueue.Count > 0) { try { // use the socket data structure to begin receiving the byte from the server socket.BeginReceive(bytesToReceive, bytesReceived, bytesToReceive.Length, SocketFlags.None, ReceiveByteCallback, null); } catch (Exception e) { ReceiveMessage exceptionMessage = receiveQueue.Dequeue(); ThreadPool.QueueUserWorkItem(o => exceptionMessage.callback(null, e, exceptionMessage.payload)); } } else { isReceiving = false; } }