private void DisplayReply(PingReply reply, ServerPingData server)
        {
            if (reply == null)
            {
                server.ErrorCount++;
                return;
            }

            //Debug.Print("ping status: {0}", reply.Status);
            if (reply.Status == IPStatus.Success)
            {
                decimal totalPing = server.AvgPing * server.TestCount;

                totalPing += reply.RoundtripTime;

                server.TestCount++;

                server.AvgPing = totalPing / server.TestCount;

                if (server.MinPing == 0 || server.MinPing > reply.RoundtripTime)
                {
                    server.MinPing = reply.RoundtripTime;
                }
                if (server.MaxPing == 0 || server.MaxPing < reply.RoundtripTime)
                {
                    server.MaxPing = reply.RoundtripTime;
                }

                //Debug.Print("Address: {0}", reply.Address.ToString());
                //Debug.Print("RoundTrip time: {0}", reply.RoundtripTime);
                //Debug.Print("Time to live: {0}", reply.Options.Ttl);
                //Debug.Print("Don't fragment: {0}", reply.Options.DontFragment);
                //Debug.Print("Buffer size: {0}", reply.Buffer.Length);
            }
            else
            {
                server.ErrorCount++;
            }

            if (!PingTable.Dispatcher.CheckAccess())
            {
                this.Dispatcher.Invoke(new UpdatePingTableDelegate(UpdatePingTable));
                return;
            }
        }
        private void startPingTestServer(ServerPingData server)
        {
            if (server.TestCount + server.ErrorCount >= 10)
            {
                return;
            }

            Ping pingSender = new Ping();

            pingSender.PingCompleted += new PingCompletedEventHandler((sender, e) => PingCompletedCallback(sender, e, server));

            int timeout = 1000;

            PingOptions options = new PingOptions(64, true);

            int index = server.TestCount % server.ServerAddress.Length;

            pingSender.SendAsync(server.ServerAddress[index], timeout, options);
        }
        private void PingCompletedCallback(object sender, PingCompletedEventArgs e, ServerPingData server)
        {
            if (e.Cancelled)
            {
                //Debug.Print("Ping canceled.");

                DisplayReply(null, server);
                return;
            }

            if (e.Error != null)
            {
                //Debug.Print("Ping failed:");
                //Debug.Print(e.Error.ToString());

                DisplayReply(null, server);
                return;
            }

            PingReply reply = e.Reply;

            DisplayReply(reply, server);
            startPingTestServer(server);
        }