Index: trunk/src/sh_tools.c
===================================================================
--- trunk/src/sh_tools.c	(revision 312)
+++ trunk/src/sh_tools.c	(revision 313)
@@ -574,5 +574,5 @@
 	{
 	  if ( 0 == sl_strncmp(check_cache->address, 
-			       address, sl_strlen(address)))
+			       address, sl_strlen(address)) )
 	    {
 	      memcpy (&sinr, &((check_cache->saddr).sin), sizeof(struct sockaddr_in));
@@ -778,6 +778,5 @@
       while (check_cache && check_cache->address)
 	{
-	  if ( 0 == sl_strncmp(check_cache->address, 
-			       address, sl_strlen(address)))
+	  if ( 0 == sl_strcmp(check_cache->address, address) )
 	    {
 	      memcpy (&ss, &(check_cache->saddr), sizeof(struct sh_sockaddr));
@@ -787,9 +786,14 @@
 		  sin = &(ss.sin);
 		  sin->sin_port   = htons (port);
+		  cached = 1;
+		  break;
 		case AF_INET6:
 		  sin6 = &(ss.sin6);
 		  sin6->sin6_port  = htons (port);
+		  cached = 1;
+		  break;
+		default:
+		  break;
 		}
-	      cached = 1;
 	      break;
 	    }
@@ -814,6 +818,6 @@
 	  sl_strlcat(errmsg, address, errsiz);
 	}
-    
-  
+
+
       if (fail != (-1)) 
 	{
@@ -853,4 +857,5 @@
       hints.ai_flags |= AI_CANONNAME;
 #endif 
+      hints.ai_family   = AF_UNSPEC;
       hints.ai_socktype = SOCK_STREAM;
       sl_snprintf(sport, sizeof(sport), "%d", port);
@@ -923,45 +928,49 @@
 	  while (p != NULL)
 	    {
-	      fd = socket(p->ai_family, p->ai_socktype,
-			  p->ai_protocol);
-
-	      if (fd != (-1))
+	      if ( (SOCK_STREAM == p->ai_socktype) &&
+		   ((p->ai_family == AF_INET) || (p->ai_family == AF_INET6)) )
 		{
-		  if (retry_connect(FIL__, __LINE__, fd, 
-				    p->ai_addr, p->ai_addrlen) >= 0)
+		
+		  fd = socket(p->ai_family, SOCK_STREAM, 0);
+		  
+		  if (fd != (-1))
 		    {
-		      /* put it into the cache
-		       */
-		      check_cache          = SH_ALLOC(sizeof(sin_cache));
-		      check_cache->address = SH_ALLOC(sl_strlen(address) + 1);
-		      sl_strlcpy (check_cache->address, address, sl_strlen(address) + 1);
-
-		      sh_ipvx_save(&(check_cache->saddr), p->ai_family, p->ai_addr);
-
-		      ++cached_addr;
-	  
-		      if (conn_cache)
+		      if (retry_connect(FIL__, __LINE__, fd, 
+					p->ai_addr, p->ai_addrlen) >= 0)
 			{
-			  if (conn_cache->next)
-			    check_cache->next    = conn_cache->next;
+			  /* put it into the cache
+			   */
+			  check_cache          = SH_ALLOC(sizeof(sin_cache));
+			  check_cache->address = SH_ALLOC(sl_strlen(address) + 1);
+			  sl_strlcpy (check_cache->address, address, sl_strlen(address) + 1);
+			  
+			  sh_ipvx_save(&(check_cache->saddr), p->ai_family, p->ai_addr);
+			  
+			  ++cached_addr;
+			  
+			  if (conn_cache)
+			    {
+			      if (conn_cache->next)
+				check_cache->next    = conn_cache->next;
+			      else
+				check_cache->next    = NULL;
+			      conn_cache->next     = check_cache;
+			    }
 			  else
-			    check_cache->next    = NULL;
-			  conn_cache->next     = check_cache;
+			    {
+			      check_cache->next    = NULL;
+			      conn_cache           = check_cache;
+			    }
+			  
+			  freeaddrinfo (ai);
+			  goto end;
 			}
-		      else
-			{
-			  check_cache->next    = NULL;
-			  conn_cache           = check_cache;
-			}
-		    
-		      freeaddrinfo (ai);
-		      goto end;
+		      status = errno;
+		      sl_close_fd(FIL__, __LINE__, fd);
 		    }
-		  status = errno;
-		  sl_close_fd(FIL__, __LINE__, fd);
-		}
-	      else
-		{
-		  status = errno;
+		  else
+		    {
+		      status = errno;
+		    }
 		}
 	      p = p->ai_next;
