title: C# Server socket bind / re-bind an endpoint when Ethernet is connected / disconnected
body: <p><strong>Background:</strong>
  I  have a <code>socket</code> server that binds to a specific IP address on the machines NIC. This works fine <strong>IF</strong> the NIC is connected to the client machine. Though, what I am having trouble with is an error-scenario where if the NIC is not connected to the client machine, it throws the following error: "<code>the requested address is not valid in its context</code>". This makes sense, because since there's no connection, the IP address cannot be assigned.</p>
  
  <p><strong>Problem:</strong>
  How can I re-bind the <code>socket</code> so that once the NIC is physically connected to the client machine, the <code>socket</code> starts listening? Note that when I restart the program, it binds correctly and everything is fine. Though, I want to be able to restart the <code>socket</code> / re-bind without having to restart the program.</p>
  
  <p><strong>Server Socket connection</strong>:
  I already have code where the connection is recalled every X seconds if it is determined that the socket is not bound (done in a background service). When I call the reconnect method (shown below), I still get the error.</p>
  
  <pre><code>var listeningSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  listeningSocket.SetSocketOptions(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
  
  // exception thrown here if NIC is not physically connected
  //
  listeningSocket.Bind(new IPEndPoint(address, port));
  </code></pre>