diff --git a/include/sys.h b/include/sys.h
index dcae59d4..ec1e8f81 100644
--- a/include/sys.h
+++ b/include/sys.h
@@ -139,6 +139,7 @@ extern char OSName[256];
 #define P_EWORKING		EINPROGRESS
 #define P_EINTR         EINTR
 #define P_ETIMEDOUT     ETIMEDOUT
+#define P_ENETUNREACH	ENETUNREACH
 #define P_ENOTSOCK	ENOTSOCK
 #define P_EIO		EIO
 #define P_ECONNABORTED	ECONNABORTED
@@ -168,6 +169,7 @@ extern char OSName[256];
 #define P_EWORKING		WSAEWOULDBLOCK
 #define P_EINTR         WSAEINTR
 #define P_ETIMEDOUT     WSAETIMEDOUT
+#define P_ENETUNREACH	WSAENETUNREACH
 #define P_ENOTSOCK	WSAENOTSOCK
 #define P_EIO		EIO
 #define P_ECONNABORTED	WSAECONNABORTED
diff --git a/src/socket.c b/src/socket.c
index b256ad38..da0e7653 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -1256,16 +1256,39 @@ int connect_server(ConfigItem_link *aconf, Client *by, struct hostent *hp)
 	{
 		int errtmp = ERRNO;
 		report_error("Connect to host %s failed: %s", client);
-		if (by && IsUser(by) && !MyUser(by))
-			sendnotice(by, "*** Connect to host %s failed.", client->name);
-		fd_close(client->local->fd);
-		--OpenFiles;
-		client->local->fd = -2;
-		free_client(client);
-		SET_ERRNO(errtmp);
-		if (ERRNO == P_EINTR)
-			SET_ERRNO(P_ETIMEDOUT);
-		return -1;
+		/*
+		 * If the error is "Network unreachable" and we tried to connect via ipv6, then this box might
+		 * not support ipv6 at all! Let's try with ipv4.
+		 */
+		if(ERRNO == P_ENETUNREACH && IsIPV6(client) && !is_valid_ip(aconf->outgoing.hostname))
+		{
+			aconf->refcount++;
+			unrealdns_gethostbyname_link(aconf->outgoing.hostname, aconf, 1);
+			
+			strlcpy(client->local->sockhost, aconf->outgoing.hostname, HOSTLEN + 1);
+			if (!connect_server_helper(aconf, client))
+			{
+				report_error("Connect to host %s failed: %s", client);
+			}
+			else
+			{
+				/* We connected, so we will bail out of the cleanup */
+				errtmp = 0;
+			}
+		}
+		if(errtmp != 0)
+		{
+			if (by && IsUser(by) && !MyUser(by))
+				sendnotice(by, "*** Connect to host %s failed.", client->name);
+			fd_close(client->local->fd);
+			--OpenFiles;
+			client->local->fd = -2;
+			free_client(client);
+			SET_ERRNO(errtmp);
+			if (ERRNO == P_EINTR)
+				SET_ERRNO(P_ETIMEDOUT);
+			return -1;
+		}
 	}
 	/* The socket has been connected or connect is in progress. */
 	make_server(client);
