title: How to check the Internet connection with .NET, C#, and WPF
body: <p>I am using .NET, C# and WPF, and I need to check whether the connection is opened to a certain URL, and I can't get any code to work that I have found on the Internet.</p>
  
  <p>I tried:</p>
  
  <pre><code>Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  try
  {
      IAsyncResult result = socket.BeginConnect("localhost/myfolder/", 80, null, null);
      bool success = result.AsyncWaitHandle.WaitOne(3000, true);
      if (!success)
      {
          MessageBox.Show("Web Service is down!");
      }
      else
          MessageBox.Show("Everything seems ok");
  }
  finally
  {
      socket.Close();
  }
  </code></pre>
  
  <p>But I always get the message that everything is OK even if I shut down my local Apache server.</p>
  
  <p>I also tried:</p>
  
  <pre><code>ing ping = new Ping();
  PingReply reply;
  try
  {
      reply = ping.Send("localhost/myfolder/");
      if (reply.Status != IPStatus.Success)
          MessageBox.Show("The Internet connection is down!");
      else
          MessageBox.Show("Seems OK");
  }
  catch (Exception ex)
  {
      MessageBox.Show("Error: " + ex.Message);
  }
  </code></pre>
  
  <p>But this always gives an exception (ping seems to work only pinging the server, so localhost works but localhost/myfolder/ doesnt)</p>
  
  <p>Please how to check the connection so it would work for me?</p>