Index: kinit/wrapper.c
===================================================================
--- kinit/wrapper.c.orig
+++ kinit/wrapper.c
@@ -164,7 +164,9 @@ static int openSocket()
      sock_file[strlen(sock_file)-1] = 0;
   
   strncat(sock_file, "/socket-", MAX_SOCK_FILE - strlen(sock_file));
-  if (gethostname(sock_file+strlen(sock_file), MAX_SOCK_FILE - strlen(sock_file) - 1) != 0)
+  if( getenv("XAUTHLOCALHOSTNAME"))
+      strncat(sock_file, getenv("XAUTHLOCALHOSTNAME"), MAX_SOCK_FILE - strlen(sock_file) - 1);
+  else if (gethostname(sock_file+strlen(sock_file), MAX_SOCK_FILE - strlen(sock_file) - 1) != 0)
   {
      perror("Warning: Could not determine hostname: ");
      return -1;
Index: kinit/lnusertemp.c
===================================================================
--- kinit/lnusertemp.c.orig
+++ kinit/lnusertemp.c
@@ -39,7 +39,7 @@
 
 int check_tmp_dir(const char *tmp_dir);
 int create_link(const char *file, const char *tmp_dir);
-int build_link(const char *tmp_prefix, const char *kde_prefix);
+int build_link(const char *tmp_prefix, const char *kde_prefix, int kdehostname);
 
 int check_tmp_dir(const char *tmp_dir)
 {
@@ -89,7 +89,7 @@ int create_link(const char *file, const
 }
 
 
-int build_link(const char *tmp_prefix, const char *kde_prefix)
+int build_link(const char *tmp_prefix, const char *kde_prefix, int kdehostname)
 {
   struct passwd *pw_ent;
   char kde_tmp_dir[PATH_MAX+1];
@@ -157,10 +157,21 @@ int build_link(const char *tmp_prefix, c
   }  
 
   strncat(kde_tmp_dir, kde_prefix, PATH_MAX - strlen(kde_tmp_dir));
-  if (gethostname(kde_tmp_dir+strlen(kde_tmp_dir), PATH_MAX - strlen(kde_tmp_dir) - 1) != 0)
+
+  if( kdehostname )
+  {
+      if( getenv("XAUTHLOCALHOSTNAME"))
+          strncat(kde_tmp_dir+strlen(kde_tmp_dir), getenv("XAUTHLOCALHOSTNAME"), PATH_MAX - strlen(kde_tmp_dir) - 1);
+      else
+          return 0;
+  }
+  else
   {
-     perror("Aborting. Could not determine hostname: ");
-     exit(255);
+     if (gethostname(kde_tmp_dir+strlen(kde_tmp_dir), PATH_MAX - strlen(kde_tmp_dir) - 1) != 0)
+     {
+        perror("Could not determine hostname: ");
+        return 1;
+     }
   }
   kde_tmp_dir[sizeof(kde_tmp_dir)-1] = '\0';
 
@@ -266,7 +277,9 @@ int main(int argc, char **argv)
     kde_prefix = "/cache-"; 
   }
 
-  res = build_link(tmp_prefix, kde_prefix); 
+  res = build_link(tmp_prefix, kde_prefix, 1); 
+  if( build_link(tmp_prefix, kde_prefix, 0))
+    res = 1;
     
   free(tmp_prefix);
 
Index: kdecore/kstandarddirs.cpp
===================================================================
--- kdecore/kstandarddirs.cpp.orig
+++ kdecore/kstandarddirs.cpp
@@ -719,7 +719,10 @@ void KStandardDirs::createSpecialResourc
 {
    char hostname[256];
    hostname[0] = 0;
-   gethostname(hostname, 255);
+   if( getenv("XAUTHLOCALHOSTNAME"))
+       strlcpy(hostname, getenv("XAUTHLOCALHOSTNAME"), 255 );
+   else
+       gethostname(hostname, 255);
    QString dir = QString("%1%2-%3").arg(localkdedir()).arg(type).arg(hostname);
    char link[1024];
    link[1023] = 0;
Index: kdecore/kcrash.cpp
===================================================================
--- kdecore/kcrash.cpp.orig
+++ kdecore/kcrash.cpp
@@ -466,7 +466,9 @@ static int openSocket()
      sock_file[strlen(sock_file)-1] = 0;
   
   strncat(sock_file, "/socket-", MAX_SOCK_FILE - strlen(sock_file));
-  if (gethostname(sock_file+strlen(sock_file), MAX_SOCK_FILE - strlen(sock_file) - 1) != 0)
+  if( getenv("XAUTHLOCALHOSTNAME"))
+      strncat(sock_file, getenv("XAUTHLOCALHOSTNAME"), MAX_SOCK_FILE - strlen(sock_file) - 1);
+  else if (gethostname(sock_file+strlen(sock_file), MAX_SOCK_FILE - strlen(sock_file) - 1) != 0)
   {
      perror("Warning: Could not determine hostname: ");
      return -1;
Index: dcop/dcopserver_shutdown.c
===================================================================
--- dcop/dcopserver_shutdown.c.orig
+++ dcop/dcopserver_shutdown.c
@@ -95,7 +95,9 @@ static void getDCOPFile(char *dcop_file,
   strncat(dcop_file, "/.DCOPserver_", n);
   n -= strlen("/.DCOPserver_");
 
-  if (gethostname(dcop_file+strlen(dcop_file), n) != 0)
+  if (getenv("XAUTHLOCALHOSTNAME"))
+     strncat(dcop_file+strlen(dcop_file), getenv("XAUTHLOCALHOSTNAME"), n);
+  else if (gethostname(dcop_file+strlen(dcop_file), n) != 0)
   {
      perror("Error. Could not determine hostname: ");
      dcop_file[0] = '\0';
Index: dcop/dcopc.c
===================================================================
--- dcop/dcopc.c.orig
+++ dcop/dcopc.c
@@ -684,7 +684,9 @@ dcop_connect()
     }
 
     hostName[0] = '\0';
-    if (gethostname(hostName, sizeof(hostName)))
+    if (getenv("XAUTHLOCALHOSTNAME"))
+            strlcpy(hostName, getenv("XAUTHLOCALHOSTNAME"),sizeof(hostName)-1);
+    else if (gethostname(hostName, sizeof(hostName)))
 	    strcpy(hostName, "localhost");
     else
 	    hostName[sizeof(hostName)-1] = '\0';
Index: dcop/dcopclient.cpp
===================================================================
--- dcop/dcopclient.cpp.orig
+++ dcop/dcopclient.cpp
@@ -277,7 +277,9 @@ static QCString dcopServerFile(const QCS
     {
         char hostName[256];
         hostName[0] = '\0';
-        if (gethostname(hostName, sizeof(hostName)))
+        if (getenv("XAUTHLOCALHOSTNAME"))
+            fName += getenv("XAUTHLOCALHOSTNAME");
+        else if (gethostname(hostName, sizeof(hostName)))
         {
             fName += "localhost";
         }
Index: dcop/KDE-ICE/Xtranssock.c
===================================================================
--- dcop/KDE-ICE/Xtranssock.c.orig
+++ dcop/KDE-ICE/Xtranssock.c
@@ -1444,6 +1444,7 @@ UnixHostReallyLocal (char *host)
 
 {
     char hostnamebuf[256];
+    char* xauthlocalname = getenv("XAUTHLOCALHOSTNAME");
 
     TRANS(GetHostname) (hostnamebuf, sizeof (hostnamebuf));
 
@@ -1451,6 +1452,10 @@ UnixHostReallyLocal (char *host)
     {
 	return (1);
     }
+    else if(xauthlocalname && strcmp (xauthlocalname, host) == 0)
+    {
+       return (1);
+    }
     else
     {
 	/*
Index: kded/khostname.cpp
===================================================================
--- kded/khostname.cpp.orig
+++ kded/khostname.cpp
@@ -111,7 +111,8 @@ static QCStringList split(const QCString
 
 void KHostName::changeX()
 {
-   QString cmd = "xauth list";
+   const char* xauthlocalhostname = getenv("XAUTHLOCALHOSTNAME");
+   QString cmd = "xauth -n list";
    FILE *xFile = popen(QFile::encodeName(cmd), "r");
    if (!xFile)
    {
@@ -123,6 +124,7 @@ void KHostName::changeX()
       char buf[1024+1];
       while (!feof(xFile))
       {
+         buf[1024]='\0';
          QCString line = fgets(buf, 1024, xFile);
          if (line.length())
             line.truncate(line.length()-1); // Strip LF.
@@ -157,12 +159,17 @@ void KHostName::changeX()
       QCString newNetId = newName+netId.mid(i);
       QCString oldNetId = netId.left(i);
 
-      if(oldNetId != oldName)
-         continue;
+      if(oldNetId != oldName
+        && (!xauthlocalhostname || strcmp(xauthlocalhostname, oldNetId.data()) != 0))
+        continue;
 
-      cmd = "xauth remove "+KProcess::quote(netId);
-      system(QFile::encodeName(cmd));
-      cmd = "xauth add ";
+      // don't nuke the xauth when XAUTHLOCALHOSTNAME points to it
+      if (!xauthlocalhostname || oldNetId != xauthlocalhostname)
+      {
+        cmd = "xauth -n remove "+KProcess::quote(netId);
+        system(QFile::encodeName(cmd));
+      }
+      cmd = "xauth -n add ";
       cmd += KProcess::quote(newNetId);
       cmd += " ";
       cmd += KProcess::quote(authName);
@@ -276,7 +283,10 @@ void KHostName::changeDcop()
       }
    }
 
-   // Remove old entries
+   // Remove old entries, but only if XAUTHLOCALHOSTNAME doesn't point
+   // to it
+   char* xauthlocalhostname = getenv("XAUTHLOCALHOSTNAME");
+   if (!xauthlocalhostname || !oldNetId.contains(xauthlocalhostname))
    {
       QString cmd = "iceauth remove "+KProcess::quote("netid="+oldNetId);
       system(QFile::encodeName(cmd));
@@ -368,9 +378,7 @@ int main(int argc, char **argv)
 
    KHostName hn;
 
-   if(!getenv("XAUTHLOCALHOSTNAME"))
-       hn.changeX();
-
+   hn.changeX();
    hn.changeDcop();
    hn.changeStdDirs("socket");
    hn.changeStdDirs("tmp");