title: Stack Overflow on TcpClient (Recursion)
body: <p>Because of the repeated calls to <strong>new</strong>, the following code may produce a stack overflow exception if left as-is:</p>
  
  <pre><code>async void Connect()
  {
      try 
      {
          client = new TcpClient(ip, port);
      }
      catch (Exception e)
      {
          HandleException(e); // Most likely will be a SocketException e.g. connection refused
          Connect(); // Try to connect again
      }
  }
  </code></pre>
  
  <p>While I could reduce the chances of this happening by e.g. adding a maximum number of retries before giving up and exiting the recursion, I'd rather write it so that memory is freed and stack overflow exceptions don't happen.</p>
  
  <p>I thought about putting it in a while loop, but I think that will have the same problem, e.g.</p>
  
  <pre><code>while (!connected)
  {
      try
      {
          client = new TcpClient(ip, port);
          connected = true;
      }
      catch (Exception e)
      {
          HandleException(e);
      }
  }
  </code></pre>
  
  <p>Is there a better way to avoid stack overflow exceptions here aside from having an arbitrarily defined maximum number of retries?</p>