title: PingReply is null when using SendAsync
body: <p>I am building a server selection screen and have a bunch of IP addresses that need to be pinged in order to know the remote hosts are reachable and to establish a TCP connection afterwards to request their data (like connected playerCount and MOTD, think of Minecraft Multiplayer Selection screen, you don't connect to the game, only requesting data from the endpoint).</p>
  <p>The problem is that Ping is always unsuccessful, because the <code>PingReply</code> is null.
  For generic use cases, I have wrapped the call with an event that gets fired when the Ping Reply is received back to the sender.</p>
  <pre><code>public static async void PingNET(string address, int timeOutMs, System.Action&lt;PingReply&gt; onCompleted)
  {
      await Task.Run(() =&gt; { //to continue unity main thread, start new thread
      AutoResetEvent waiter = new AutoResetEvent(false);
      var pingSender = new System.Net.NetworkInformation.Ping ();
  
      // call handler when PingCompleted event is raised
      pingSender.PingCompleted += (sender, e) =&gt;
      {
          Debug.LogError (&quot;Error and reply is always null! See: Error is null?: &quot; 
              + (e.Error == null) + &quot; - Reply is null?: &quot; + (e.Reply == null));
          // If the operation was canceled, display a message to the user.
          if (e.Cancelled) {
             Debug.LogWarning(&quot;Ping canceled.&quot;);
          }
  
          // If an error occurred, display the exception to the user.
          if (e.Error != null) {
              Debug.LogError (&quot;Ping failed:&quot; + e.Error.ToString ());
          }
  
          if(onCompleted != null)
              onCompleted(e.Reply);
      };
  
      // Send the ping asynchronously (uses internal new thread, 
      // so program does not freeze until reply received)
      pingSender.SendAsync(address, timeOutMs, waiter);
      });
  }
  </code></pre>
  <p>Unity Test Runner Unit Test:</p>
  <pre><code>[Test]
  public void PingTest(){
      List&lt;string&gt; addresses = new List&lt;string&gt;(){
          &quot;127.0.0.1&quot;, &quot;localhost&quot;, &quot;www.stackoverflow.com&quot;, &quot;www.google.com&quot;
      };
      for (int i = 0; i &lt; addresses.Count; i++)
      {
          Ping(addresses[i]);
      }
  }
  
  private void Ping(string address){
      NetworkUtils.PingNET(address, 10000, new System.Action&lt;PingReply&gt;((PingReply reply) =&gt; {
          if (reply != null &amp;&amp; reply.Status == IPStatus.Success)
          {
              Debug.Log($&quot;Address: {reply.Address.ToString()}&quot;);
              Debug.Log($&quot;RoundTrip time: {reply.RoundtripTime}&quot;);
          }
          else //this gets called obviously
          {
              Debug.LogError(&quot;PING REPLY ERROR &quot; + reply?.Status); 
          } 
      }));
  }
  </code></pre>
  <hr />
  <p><strong>Side Info: (Not directly tied to the question)</strong></p>
  <p>The reason why I use <code>Ping</code> in the first place is to prevent freeze (deadlock) on the client when the server goes offline while the connection is still tied to it and requesting data to the now unavailable remote host endpoint of the server. I could notify all connected clients that the server is shutting down, but this of course will not work when the server immediately lost connection to the internet.</p>
  <p>So all in all, when the ping fails, it should handle in the UI logic to display the server is unreachable. Otherwise if it was successful, a TCP connection to the server is created to send a request. The server that listens on the same port sends a response with the data (playerCount, MOTD, etc.). After that, the client deseralizes the byte array to the datatypes and fires an event, which is subscribed by the UI to gather the deserealized data and display those.</p>
  <p>The sending/receiving part with UI already works, just the ping not.</p>
  <p>Is this process overcomplicated? Let me know if it could be simplified.</p>
  <p>Any help is greatly appreciated.</p>