title: How do I decide which IPv4 address to use?
body: <p>In an environment with multiple IPv4 addresses, how do I choose which one to listen on? I've had mixed luck with the following code:</p>
  
  <pre><code>foreach(IPAddress addr in Dns.GetHostAddresses(Dns.GetHostName()))
      if (addr.AddressFamily == AddressFamily.InterNetwork)
          if (!IPAddress.IsLoopback(addr))
              return addr.ToString();
  </code></pre>
  
  <p>Since it does not consider the following interface adapter to be a loopback address:</p>
  
  <pre><code>Ethernet adapter loopback:
  Connection-specific DNS Suffix  . : 
  Description . . . . . . . . . . . : Microsoft Loopback Adapter
  Physical Address. . . . . . . . . : XX.XX.XX.XX.XX.XX
  DHCP Enabled. . . . . . . . . . . : Yes
  Autoconfiguration Enabled . . . . : Yes
  Autoconfiguration IPv4 Address. . : 169.254.XXX.XXX(Preferred) 
  Subnet Mask . . . . . . . . . . . : 255.255.0.0
  Default Gateway . . . . . . . . . : 
  DNS Servers . . . . . . . . . . . : XXX: XXX: XXX:XXXX::XXX
                                      XXX: XXX: XXX:XXXX::XXX
                                      XXX: XXX: XXX:XXXX::XXX
  NetBIOS over Tcpip. . . . . . . . : Enabled
  </code></pre>
  
  <p>I've seen some programmers assume a <code>NetworkInterface</code> named "Local Area Connection" exists and is always the correct interface, but I don't think that's the right approach.</p>