title: Get domain name of incoming connection
body: <p>I don't find any information on this topic on the internet and asked here. For example I have server with IP 1.1.1.1 and 2.2.2.2 and two domain names pointing to it one.example.com and example2.net, and socke listening on port 1234 for incoming connections.</p>
  
  <p>For example:</p>
  
  <p>C/C++:</p>
  
  <pre><code>listenfd=socket(AF_INET, SOCK_STREAM, 0);
  bind(...);
  listen(...);
  while(...) accept(...);
  </code></pre>
  
  <p>or Java:</p>
  
  <pre><code>ServerSocket socket = new ServerSocket(1234);
  while(...) {
      Socket connectionSocket = welcomeSocket.accept();
      ...
  }
  </code></pre>
  
  <p>When client accepted on my socket I need to know what domain name/IP is used by the client to connect. It may be one.example.com or example2.net and/or IP 1.1.1.1 or 2.2.2.2 (if connected using IP only).</p>
  
  <p>Apache somehow determined ip/domain of incoming reques, and I need to do such thing in pure socket code. C++ (main) or Java (or any other) accepted, I need to know mechaniics of this. </p>