Index: lib/misc.c
===================================================================
--- lib/misc.c	(revision f32c14c649e854fbfb7b8e495e2ebc8459ee5b6f)
+++ lib/misc.c	(revision ffdf2e71d9e67980727aa994b77fca36ef5246c6)
@@ -508,8 +508,9 @@
 }
 
-struct ns_srv_reply *srv_lookup( char *service, char *protocol, char *domain )
+struct ns_srv_reply **srv_lookup( char *service, char *protocol, char *domain )
 {	
+	struct ns_srv_reply **replies = NULL;
+#ifdef HAVE_RESOLV_A
 	struct ns_srv_reply *reply = NULL;
-#ifdef HAVE_RESOLV_A
 	char name[1024];
 	unsigned char querybuf[1024];
@@ -517,5 +518,5 @@
 	ns_msg nsh;
 	ns_rr rr;
-	int i, len, size;
+	int i, n, len, size;
 	
 	g_snprintf( name, sizeof( name ), "_%s._%s.%s", service, protocol, domain );
@@ -527,35 +528,54 @@
 		return NULL;
 	
-	if( ns_parserr( &nsh, ns_s_an, 0, &rr ) != 0 )
-		return NULL;
-	
-	size = ns_rr_rdlen( rr );
-	buf = ns_rr_rdata( rr );
-	
-	len = 0;
-	for( i = 6; i < size && buf[i]; i += buf[i] + 1 )
-		len += buf[i] + 1;
-	
-	if( i > size )
-		return NULL;
-	
-	reply = g_malloc( sizeof( struct ns_srv_reply ) + len );
-	memcpy( reply->name, buf + 7, len );
-	
-	for( i = buf[6]; i < len && buf[7+i]; i += buf[7+i] + 1 )
-		reply->name[i] = '.';
-	
-	if( i > len )
-	{
-		g_free( reply );
-		return NULL;
-	}
-	
-	reply->prio = ( buf[0] << 8 ) | buf[1];
-	reply->weight = ( buf[2] << 8 ) | buf[3];
-	reply->port = ( buf[4] << 8 ) | buf[5];
+	n = 0;
+	while( ns_parserr( &nsh, ns_s_an, n, &rr ) == 0 )
+	{
+		size = ns_rr_rdlen( rr );
+		buf = ns_rr_rdata( rr );
+		
+		len = 0;
+		for( i = 6; i < size && buf[i]; i += buf[i] + 1 )
+			len += buf[i] + 1;
+		
+		if( i > size )
+			break;
+		
+		reply = g_malloc( sizeof( struct ns_srv_reply ) + len );
+		memcpy( reply->name, buf + 7, len );
+		
+		for( i = buf[6]; i < len && buf[7+i]; i += buf[7+i] + 1 )
+			reply->name[i] = '.';
+		
+		if( i > len )
+		{
+			g_free( reply );
+			break;
+		}
+		
+		reply->prio = ( buf[0] << 8 ) | buf[1];
+		reply->weight = ( buf[2] << 8 ) | buf[3];
+		reply->port = ( buf[4] << 8 ) | buf[5];
+		
+		n ++;
+		replies = g_renew( struct ns_srv_reply *, replies, n + 1 );
+		replies[n-1] = reply;
+	}
+	if( replies )
+		replies[n] = NULL;
 #endif
 	
-	return reply;
+	return replies;
+}
+
+void srv_free( struct ns_srv_reply **srv )
+{
+	int i;
+	
+	if( srv == NULL )
+		return;
+	
+	for( i = 0; srv[i]; i ++ )
+		g_free( srv[i] );
+	g_free( srv );
 }
 
Index: lib/misc.h
===================================================================
--- lib/misc.h	(revision f32c14c649e854fbfb7b8e495e2ebc8459ee5b6f)
+++ lib/misc.h	(revision ffdf2e71d9e67980727aa994b77fca36ef5246c6)
@@ -61,5 +61,6 @@
 G_MODULE_EXPORT int bool2int( char *value );
 
-G_MODULE_EXPORT struct ns_srv_reply *srv_lookup( char *service, char *protocol, char *domain );
+G_MODULE_EXPORT struct ns_srv_reply **srv_lookup( char *service, char *protocol, char *domain );
+G_MODULE_EXPORT void srv_free( struct ns_srv_reply **srv );
 
 G_MODULE_EXPORT char *word_wrap( const char *msg, int line_len );
Index: protocols/jabber/jabber.c
===================================================================
--- protocols/jabber/jabber.c	(revision f32c14c649e854fbfb7b8e495e2ebc8459ee5b6f)
+++ protocols/jabber/jabber.c	(revision ffdf2e71d9e67980727aa994b77fca36ef5246c6)
@@ -96,5 +96,5 @@
 	struct im_connection *ic = imcb_new( acc );
 	struct jabber_data *jd = g_new0( struct jabber_data, 1 );
-	struct ns_srv_reply *srv = NULL;
+	struct ns_srv_reply **srvl = NULL, *srv;
 	char *connect_to, *s;
 	int i;
@@ -196,7 +196,17 @@
 	if( acc->server && *acc->server )
 		connect_to = acc->server;
-	else if( ( srv = srv_lookup( "xmpp-client", "tcp", jd->server ) ) ||
-		 ( srv = srv_lookup( "jabber-client", "tcp", jd->server ) ) )
+	else if( ( srvl = srv_lookup( "xmpp-client", "tcp", jd->server ) ) ||
+	         ( srvl = srv_lookup( "jabber-client", "tcp", jd->server ) ) )
+	{
+		/* Find the lowest-priority one. These usually come
+		   back in random/shuffled order. Not looking at
+		   weights etc for now. */
+		srv = *srvl;
+		for( i = 1; srvl[i]; i ++ )
+			if( srvl[i]->prio < srv->prio )
+				srv = srvl[i];
+		
 		connect_to = srv->name;
+	}
 	else
 		connect_to = jd->server;
@@ -227,5 +237,5 @@
 		jd->fd = proxy_connect( connect_to, srv ? srv->port : set_getint( &acc->set, "port" ), jabber_connected_plain, ic );
 	}
-	g_free( srv );
+	srv_free( srvl );
 	
 	if( jd->fd == -1 )
