private XAuthority get_authority () {

    XAuthority[] auths = XAuthority.get_authorities();

    // Fetch hostname.
    if (hostname == null || hostname.equals("")
        ||hostname.equals ("localhost")) {
      // Translate localhost hostnames to the real hostname of this host.
      try {
        InetAddress local = InetAddress.getLocalHost ();
        hostname = local.getHostName ();
      } catch (UnknownHostException ex) {
        ex.printStackTrace();
      }
    }

    // Fetch display no.
    String display_no_str = String.valueOf (display_no);

    // Find the XAuthority that matches the hostname and display no.
    XAuthority found = null;
    for (int i = 0; i < auths.length; i++) {
      XAuthority auth = auths[i];
      try {
        if (auth.hostname != null && auth.display.equals (display_no_str)
            && InetAddress.getByName(auth.hostname)
                 .equals(InetAddress.getByName(hostname))) {
          found = auth;
          break;
        }
      } catch (UnknownHostException ex) {
         System.err.println("warning unknown host :"+auth.hostname);
      }
    }
    return found;
  }