diff --git a/libpurple/protocols/jabber/jabber.c b/libpurple/protocols/jabber/jabber.c
--- a/libpurple/protocols/jabber/jabber.c
+++ b/libpurple/protocols/jabber/jabber.c
@@ -164,9 +164,11 @@ static void jabber_bind_result_cb(Jabber
 	jabber_session_init(js);
 }
 
-static char *jabber_prep_resource(char *input) {
-	char hostname[256], /* current hostname */
-		 *dot = NULL;
+static char *
+jabber_prep_resource(char *input)
+{
+	const gchar *hostname = NULL, *dot = NULL;
+	gchar *result = NULL;
 
 	/* Empty resource == don't send any */
 	if (input == NULL || *input == '\0')
@@ -176,25 +178,20 @@ static char *jabber_prep_resource(char *
 		return g_strdup(input);
 
 	/* Replace __HOSTNAME__ with hostname */
-	if (gethostname(hostname, sizeof(hostname) - 1)) {
-		purple_debug_warning("jabber", "gethostname: %s\n", g_strerror(errno));
-		/* according to glibc doc, the only time an error is returned
-		   is if the hostname is longer than the buffer, in which case
-		   glibc 2.2+ would still fill the buffer with partial
-		   hostname, so maybe we want to detect that and use it
-		   instead
-		*/
-		g_strlcpy(hostname, "localhost", sizeof(hostname));
-	}
-	hostname[sizeof(hostname) - 1] = '\0';
+	hostname = g_get_host_name();
 
 	/* We want only the short hostname, not the FQDN - this will prevent the
 	 * resource string from being unreasonably long on systems which stuff the
 	 * whole FQDN in the hostname */
-	if((dot = strchr(hostname, '.')))
-		*dot = '\0';
-
-	return purple_strreplace(input, "__HOSTNAME__", hostname);
+	if ((dot = strchr(hostname, '.')) != NULL) {
+		gchar *short_hostname = g_strndup(hostname, dot - hostname);
+		result = purple_strreplace(input, "__HOSTNAME__", short_hostname);
+		g_free(short_hostname);
+	} else {
+		result = purple_strreplace(input, "__HOSTNAME__", hostname);
+	}
+
+	return result;
 }
 
 static gboolean
diff --git a/libpurple/protocols/zephyr/ZLocations.c b/libpurple/protocols/zephyr/ZLocations.c
--- a/libpurple/protocols/zephyr/ZLocations.c
+++ b/libpurple/protocols/zephyr/ZLocations.c
@@ -79,10 +79,7 @@ Z_SendLocation(char *class, char *opcode
      */
 
     if (!reenter) {
-	    if (gethostname(host, MAXHOSTNAMELEN) < 0)
-		    return (errno);
-
-	    hent = gethostbyname(host);
+	    hent = gethostbyname(g_get_host_name());
 	    if (hent) {
 	      (void) strncpy(host, hent->h_name, sizeof(host));
 	      host[sizeof(host) - 1] = '\0';
diff --git a/libpurple/protocols/zephyr/zephyr.c b/libpurple/protocols/zephyr/zephyr.c
--- a/libpurple/protocols/zephyr/zephyr.c
+++ b/libpurple/protocols/zephyr/zephyr.c
@@ -487,20 +487,18 @@ static void zephyr_inithosts(zephyr_acco
 	/* XXX This code may not be Win32 clean */
 	struct hostent *hent;
 
-	if (gethostname(zephyr->ourhost, sizeof(zephyr->ourhost)) == -1) {
-		purple_debug_error("zephyr", "unable to retrieve hostname, %%host%% and %%canon%% will be wrong in subscriptions and have been set to unknown\n");
-		g_strlcpy(zephyr->ourhost, "unknown", sizeof(zephyr->ourhost));
-		g_strlcpy(zephyr->ourhostcanon, "unknown", sizeof(zephyr->ourhostcanon));
+	zephyr->ourhost = g_strdup(g_get_host_name());
+	if (!(hent = gethostbyname(zephyr->ourhost))) {
+		purple_debug_error("zephyr",
+		                   "unable to resolve hostname, %%canon%% will be "
+		                   "wrong in subscriptions and has been set to the "
+		                   "value of %%host%%, %s",
+		                   zephyr->ourhost);
+		zephyr->ourhostcanon = g_strdup(zephyr->ourhost);
 		return;
 	}
 
-	if (!(hent = gethostbyname(zephyr->ourhost))) {
-		purple_debug_error("zephyr", "unable to resolve hostname, %%canon%% will be wrong in subscriptions.and has been set to the value of %%host%%, %s\n",zephyr->ourhost);
-		g_strlcpy(zephyr->ourhostcanon, zephyr->ourhost, sizeof(zephyr->ourhostcanon));
-		return;
-	}
-
-	g_strlcpy(zephyr->ourhostcanon, hent->h_name, sizeof(zephyr->ourhostcanon));
+	zephyr->ourhostcanon = g_strdup(hent->h_name);
 }
 
 static void process_zsubs(zephyr_account *zephyr)
@@ -860,6 +858,9 @@ static void zephyr_close(PurpleConnectio
 		g_source_remove(zephyr->loctimer);
 	zephyr->loctimer = 0;
 	zephyr->close(zephyr);
+
+	g_clear_pointer(&zephyr->ourhost, g_free);
+	g_clear_pointer(&zephyr->ourhostcanon, g_free);
 }
 
 static gboolean
diff --git a/libpurple/protocols/zephyr/zephyr_account.h b/libpurple/protocols/zephyr/zephyr_account.h
--- a/libpurple/protocols/zephyr/zephyr_account.h
+++ b/libpurple/protocols/zephyr/zephyr_account.h
@@ -63,8 +63,8 @@ struct _zephyr_account {
 	GSList *subscrips;
 	int last_id;
 	unsigned short port;
-	char ourhost[HOST_NAME_MAX + 1];
-	char ourhostcanon[HOST_NAME_MAX + 1];
+	gchar *ourhost;
+	gchar *ourhostcanon;
 	zephyr_connection_type connection_type;
 	char *exposure;
 	GSubprocess *tzc_proc;
diff --git a/libpurple/win32/libc_interface.h b/libpurple/win32/libc_interface.h
--- a/libpurple/win32/libc_interface.h
+++ b/libpurple/win32/libc_interface.h
@@ -57,9 +57,6 @@ wpurple_fcntl( fd, command, ##__VA_ARGS_
 #define gethostbyname( name ) \
 wpurple_gethostbyname( name )
 
-#define gethostname( name, size ) \
-wpurple_gethostname( name, size )
-
 /* stdio.h */
 #if !defined(__MINGW64_VERSION_MAJOR) || __MINGW64_VERSION_MAJOR < 3 || \
 	!defined(IS_WIN32_CROSS_COMPILED)
diff --git a/libpurple/win32/libc_interface.c b/libpurple/win32/libc_interface.c
--- a/libpurple/win32/libc_interface.c
+++ b/libpurple/win32/libc_interface.c
@@ -108,13 +108,3 @@ struct hostent* wpurple_gethostbyname(co
 	}
 	return hp;
 }
-
-/* unistd.h */
-
-int wpurple_gethostname(char *name, size_t size) {
-        if(gethostname(name, size) == SOCKET_ERROR) {
-                errno = WSAGetLastError();
-			return -1;
-        }
-        return 0;
-}
diff --git a/libpurple/win32/libc_internal.h b/libpurple/win32/libc_internal.h
--- a/libpurple/win32/libc_internal.h
+++ b/libpurple/win32/libc_internal.h
@@ -38,9 +38,6 @@ int wpurple_fcntl(int socket, int comman
 #define F_SETFL 4
 #define O_NONBLOCK 04000
 
-/* unistd.h */
-int wpurple_gethostname(char *name, size_t size);
-
 G_END_DECLS
 
 #endif /* PURPLE_WIN32_LIBC_INTERNAL */
