public static void OnReceive(IAsyncResult ar) { try { IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0); EndPoint epSender = (EndPoint)ipeSender; serverSocket.EndReceiveFrom(ar, ref epSender); string strReceived = Encoding.UTF8.GetString(byteData); strReceived = strReceived.Substring(0, strReceived.IndexOf("\0")); string echo = ErrorManager.GetCurrentState(strReceived); if (echo != null) { serverSocket.SendTo(Encoding.UTF8.GetBytes(echo), epSender); } Debug.WriteLine( string.Format("UDPServer.OnReceive -> received = {0}" , strReceived)); Array.Clear(byteData, 0, byteData.Length); //int i = strReceived.IndexOf("\0"); //Manualstate.WaitOne(); //Manualstate.Reset(); ////todo here should deal with the received string //sbuilder.Append(strReceived.Substring(0, i)); //Manualstate.Set(); //Start listening to the message send by the user serverSocket.BeginReceiveFrom(byteData, 0, byteData.Length, SocketFlags.None, ref epSender, new AsyncCallback(OnReceive), epSender); } catch (Exception ex) { Debug.WriteLine( string.Format("UDPServer.OnReceive -> error = {0}" , ex.Message)); } }