private void OnReceive(IAsyncResult ar) { var socket = (Socket)ar.AsyncState; int received = testSocket.EndReceive(ar); var receivedData = new byte[received]; Array.Copy(byteData, receivedData, received); string desc = Encoding.ASCII.GetString(receivedData, 0, received); if (desc[desc.Length - 1] == 'F') { Robot robot = new Robot(); string path = robot.realTimeShortestPath(desc); this.shortestPath = "A"; if (path != null) { this.shortestPath += path; } else { //no solution this.shortestPath = "B"; } sendShortestPath(this.shortestPath); } if (desc.Length != 0) { OnReceivingData(desc); } testSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, OnReceive, testSocket); }