title: IP address order in gethostbyname function
body: <p>Since I got no answer on my previous question, I will rephrase it.</p>
  
  <p>What order of IP addresses (in case when IP addresses are bound to one interface) is used when doing gethostbyname() using PC name (NetBIOS name)?</p>
  
  <p>I have this code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;winsock.h&gt;
  #pragma comment(lib, "Ws2_32.lib")
  
  int main()
  {
      char hostname[255];
      struct hostent *he;
      struct in_addr **addr_list;
  
      WSAData data;
      WSAStartup(MAKEWORD(2, 2), &amp;data);
  
      gethostname(hostname, 255);
      std::cout &lt;&lt; "Host name: " &lt;&lt; hostname &lt;&lt; std::endl;
  
      if ((he = gethostbyname(hostname)) == NULL) {
          std::cout &lt;&lt; "gethostbyname error" &lt;&lt; std::endl;
      } else {
          std::cout &lt;&lt; "IP addresses: "  &lt;&lt; std::endl;
          addr_list = (struct in_addr **)he-&gt;h_addr_list;
          for(int i = 0; addr_list[i] != NULL; i++) {
              std::cout &lt;&lt; inet_ntoa(*addr_list[i]) &lt;&lt; std::endl;
          }
      }
      std::cin.get();
  }
  </code></pre>
  
  <p>And it gives me different results on Windows Server 2012 and Windows Server 2008 / Windows 7. On my home PC with Windows 7, ascending order is used:</p>
  
  <pre><code>Host name: SplattWin
  IP addresses:
  192.168.1.140
  192.168.3.1
  192.168.3.2
  192.168.3.3
  192.168.3.4
  </code></pre>
  
  <p>However, on Windows server 2012 it gives me IP addresses in descending order:</p>
  
  <pre><code>Host name: WinServ
  IP addresses:
  1.1.1.4
  1.1.1.3
  1.1.1.2
  1.1.1.1
  </code></pre>
  
  <p>Is there any way to reorder it? I tried skipassource flag when I added these IP addresses but it doesn't seem to work in this case.</p>
  
  <p>I have third party software that is using gethostname() followed by gethostbyname() to determine it's own IP address (it takes first from the list).  And it's really frustrating that you need to change settings and client side every time you add new IP address to your system.</p>