title: Keep trying to talk to server when the Internet is down
body: <p>So my application is exchanging request/responses with a server (no problems), until the internet connection dies for a couple of seconds, then comes back. Then a code like this:</p>
  
  <p><code>response = (HttpWebResponse)request.GetResponse();</code></p>
  
  <p>will throw an exception, with a status like <code>ReceiveFailure</code>, <code>ConnectFailure</code>, <code>KeepAliveFailure</code> etc.</p>
  
  <p>Now, it's quite important that if the internet connection comes back, I am able to continue communicating with the server, otherwise I'd have to start again from the beginning and that will take a long time.</p>
  
  <p>How would you go about resuming this communication when the internet is back? </p>
  
  <p>At the moment, I keep on checking for a possibility to communicate with the server, until it is possible (at least theoretically). My code attempt looks like this:</p>
  
  <pre><code>try
  {
      response = (HttpWebResponse)request.GetResponse();
  }
  catch (WebException ex)
  {
      // We have a problem receiving stuff from the server. 
      // We'll keep on trying for a while
      if (ex.Status == WebExceptionStatus.ReceiveFailure || 
          ex.Status == WebExceptionStatus.ConnectFailure || 
          ex.Status == WebExceptionStatus.KeepAliveFailure)
      {
          bool stillNoInternet = true;
  
          // keep trying to talk to the server
          while (stillNoInternet)
          {
              try
              {
                  response = (HttpWebResponse)request.GetResponse();
                  stillNoInternet = false;
              }
              catch
              {
                  stillNoInternet = true;
              }
          }
      }
  }
  </code></pre>
  
  <p>However, the problem is that the second try-catch statement keeps throwing an exception even when the internet is back.</p>
  
  <p>What am I doing wrong? Is there another way to go about fixing this?</p>
  
  <p>Thanks!</p>