code: public static String resolveHostName(String hostName) {
      Boolean knownLoopback = null;
      try {
        knownLoopback = InetAddress.getByName(hostName).isLoopbackAddress();
      } catch (UnknownHostException ex) {
        knownLoopback = (hostName.equals("127.0.0.1") || hostName.equals("localhost"));
      }
      if (knownLoopback) {
        try {
          return InetAddress.getLocalHost().getCanonicalHostName();
        } catch (UnknownHostException ex) {
          Logger.getLogger(ServerAddress.class.getName()).log(Level.SEVERE, null, ex);
        }
      }
      return hostName;
    }
docs: Resolves a hostname. If the hostname is a loopback address (e.g. 127.0.0.1 or localhost), it
  will be converted to the fully-qualified domain-name of the local address. If the hostname is
  <em>not</em> a loopback address, the original hostName parameter will be returned, unchanged.
  
  @param hostName the hostname to be resolved.
  
  @return <code>String</code> the resolved hostname.