title: Get hostname and IP address of the remote client connected in a different network
body: <p>How do I retrieve IP address and hostname of the peer client requesting connection to my server.Both client and Server are connected in different networks</p>
  
  <p>I tried getpeername()..But it gives the right IP address when both client and server are connected in same network.</p>
  
  <pre><code>Socket* SocketServer::Accept() 
  {
  
      struct sockaddr_in cli_addr;
      struct hostent *hp;
     int clilen = 0;    
      clilen = sizeof (cli_addr);
  
      SOCKET new_sock = accept(s_, (struct sockaddr *) &amp;cli_addr, &amp;clilen);
      if (new_sock == INVALID_SOCKET) 
      {
          SetLastError( WSAGetLastError() );
         // std::cout&lt;&lt;"Socket invalid due to :"&lt;&lt;WSAGetLastError()&lt;&lt;endl;
          WSACleanup();
      }
      Socket* r = new Socket(new_sock);
      //std::cout&lt;&lt;" Accepted Connection"&lt;&lt;endl;
  
    int ret = getpeername(new_sock, (struct sockaddr *)&amp;cli_addr, &amp;clilen);
  
     struct sockaddr_in *s = (struct sockaddr_in *)&amp;cli_addr;
     std::string ip = inet_ntoa(cli_addr.sin_addr);
  
    //cout &lt;&lt; " The IP address from the accept is " &lt;&lt; ip &lt;&lt; std::endl;
  
  
    hp = gethostbyaddr((char *)&amp;cli_addr.sin_addr,sizeof(cli_addr.sin_addr),AF_INET);
    std::string cliname;
    if (hp!=NULL)
     cliname  = hp-&gt;h_name;
     r-&gt;setDNS(cliname);
     r-&gt;setIP(ip);
      return r;
  }
  </code></pre>