diff --git a/gst/udp/gstudpnetutils.c b/gst/udp/gstudpnetutils.c
index 71828d3..f2e0151 100644
--- a/gst/udp/gstudpnetutils.c
+++ b/gst/udp/gstudpnetutils.c
@@ -79,7 +79,7 @@ gst_udp_get_sockaddr_length (struct sockaddr_storage *addr)
 int
 gst_udp_get_addr (const char *hostname, int port, struct sockaddr_storage *addr)
 {
-  struct addrinfo hints, *res, *nres;
+  struct addrinfo hints, *res, *nres, *found;
   char service[NI_MAXSERV];
   int ret;
 
@@ -95,14 +95,19 @@ gst_udp_get_addr (const char *hostname, int port, struct sockaddr_storage *addr)
   }
 
   nres = res;
+  found = NULL;
   while (nres) {
-    if (nres->ai_family == AF_INET || nres->ai_family == AF_INET6)
+    if (nres->ai_family == AF_INET) {
+      found = nres;
       break;
+    } else if (!found && nres->ai_family == AF_INET6)
+      found = nres;
+    }
     nres = nres->ai_next;
   }
 
-  if (nres) {
-    memcpy (addr, nres->ai_addr, nres->ai_addrlen);
+  if (found) {
+    memcpy (addr, found->ai_addr, found->ai_addrlen);
   } else {
     ret = EAI_ADDRFAMILY;
   }
