Index: tools/netsource.c
===================================================================
--- tools/netsource.c	(revision 4522)
+++ tools/netsource.c	(working copy)
@@ -49,6 +49,7 @@
 
 
 #include <jack/jack.h>
+#include <jack/internal.h>
 
 //#include <net_driver.h>
 #include <netjack_packet.h>
@@ -95,13 +96,8 @@
 
 int outsockfd;
 int insockfd;
-#ifdef WIN32
-struct sockaddr_in destaddr;
-struct sockaddr_in bindaddr;
-#else
-struct sockaddr destaddr;
-struct sockaddr bindaddr;
-#endif
+struct sockaddr_storage destaddr;
+struct sockaddr_storage bindaddr;
 
 int sync_state;
 jack_transport_state_t last_transport_state;
@@ -324,7 +320,7 @@
 	    if (cont_miss < 3*latency+5) {
 		    int r;
 		    for( r=0; r<redundancy; r++ )
-			    netjack_sendto (outsockfd, (char *) packet_buf_tx, tx_bufsize, 0, &destaddr, sizeof (destaddr), mtu);
+		      netjack_sendto (outsockfd, (char *) packet_buf_tx, tx_bufsize, 0, (struct sockaddr *)&destaddr, sizeof (destaddr), mtu);
 	    }
 	    else if (cont_miss > 50+5*latency)
 	    {
@@ -463,7 +459,7 @@
 	    if (cont_miss < 3*latency+5) {
 		    int r;
 		    for( r=0; r<redundancy; r++ )
-			    netjack_sendto (outsockfd, (char *) packet_buf_tx, tx_bufsize, 0, &destaddr, sizeof (destaddr), mtu);
+		      netjack_sendto (outsockfd, (char *) packet_buf_tx, tx_bufsize, 0, (struct sockaddr *)&destaddr, sizeof (destaddr), mtu);
 	    }
 	    else if (cont_miss > 50+5*latency)
 	    {
@@ -490,27 +486,37 @@
     exit (1);
 }
 
-void
-init_sockaddr_in (struct sockaddr_in *name , const char *hostname , uint16_t port)
+int
+init_sockaddr (struct sockaddr *name , const char *hostname , uint16_t port, int af)
 {
-    name->sin_family = AF_INET ;
-    name->sin_port = htons (port);
-    if (hostname)
+    struct addrinfo hints;
+    struct addrinfo *res;
+    char port_str[8];
+    int r;
+    
+    memset(&hints, 0, sizeof(struct addrinfo));
+    hints.ai_family = af;
+    hints.ai_socktype = SOCK_DGRAM;
+    hints.ai_flags = AI_NUMERICSERV;
+    hints.ai_flags |= AI_PASSIVE; // Ignored if hostname is not-NULL
+    
+    sprintf(port_str, "%" PRIu16, port);
+    
+    r = getaddrinfo(hostname, port_str, &hints, &res);
+    if (r != 0)
     {
-        struct hostent *hostinfo = gethostbyname (hostname);
-        if (hostinfo == NULL) {
-            fprintf (stderr, "init_sockaddr_in: unknown host: %s.\n", hostname);
-	    fflush( stderr );
-	}
-#ifdef WIN32
-        name->sin_addr.s_addr = inet_addr( hostname );
-#else
-        name->sin_addr = *(struct in_addr *) hostinfo->h_addr ;
-#endif
+        fprintf(stderr, "Error getting address information for host name `%s': %s\n",
+		hostname, gai_strerror(r));
+	fflush(stderr);
+	return -1;
     }
-    else
-        name->sin_addr.s_addr = htonl (INADDR_ANY) ;
-
+    
+    memcpy(name, res->ai_addr, res->ai_addrlen);
+    
+    r = res->ai_family;
+    freeaddrinfo(res);
+    
+    return r;
 }
 
 void
@@ -535,6 +541,7 @@
 	"  -e - skip host-to-network endianness conversion\n"
         "  -N <jack name> - Reports a different name to jack\n"
         "  -s <server name> - The name of the local jack server\n"
+        "  -4 - Use only IPv4\n"
         "\n");
 }
 
@@ -560,6 +567,8 @@
     /* heh ? these are only the copies of them ;)                 */
     int statecopy_connected, statecopy_latency, statecopy_netxruns;
     jack_nframes_t net_period;
+    int ipv4_only = FALSE;
+    int af;
     /* Argument parsing stuff */
     extern char *optarg;
     extern int optind, optopt;
@@ -576,7 +585,7 @@
     sprintf(client_name, "netjack");
     sprintf(peer_ip, "localhost");
 
-    while ((c = getopt (argc, argv, ":h:H:o:i:O:I:n:p:r:B:b:c:m:R:e:N:s:")) != -1)
+    while ((c = getopt (argc, argv, "hH:o:i:O:I:n:p:r:B:b:c:m:R:eN:s:4")) != -1)
     {
         switch (c)
         {
@@ -648,6 +657,9 @@
                 strcpy (server_name, optarg);
                 options |= JackServerName;
                 break;
+	    case '4':
+	        ipv4_only = TRUE;
+		break;
             case ':':
                 fprintf (stderr, "Option -%c requires an operand\n", optopt);
                 errflg++;
@@ -666,25 +678,25 @@
     capture_channels = capture_channels_audio + capture_channels_midi;
     playback_channels = playback_channels_audio + playback_channels_midi;
 
-    outsockfd = socket (AF_INET, SOCK_DGRAM, 0);
-    insockfd = socket (AF_INET, SOCK_DGRAM, 0);
+    af = init_sockaddr ((struct sockaddr *) &destaddr, peer_ip, peer_port, ipv4_only ? AF_INET : AF_UNSPEC);
+    outsockfd = socket (af, SOCK_DGRAM, 0);
+    insockfd = socket (af, SOCK_DGRAM, 0);
 
     if( (outsockfd == -1) || (insockfd == -1) ) {
         fprintf (stderr, "cant open sockets\n" );
         return 1;
     }
 
-    init_sockaddr_in ((struct sockaddr_in *) &destaddr, peer_ip, peer_port);
     if(bind_port) {
-        init_sockaddr_in ((struct sockaddr_in *) &bindaddr, NULL, bind_port);
-        if( bind (outsockfd, &bindaddr, sizeof (bindaddr)) ) {
+        init_sockaddr ((struct sockaddr *) &bindaddr, NULL, bind_port, af);
+        if( bind (outsockfd, (struct sockaddr *)&bindaddr, sizeof (bindaddr)) ) {
 		fprintf (stderr, "bind failure\n" );
 	}
     }
     if(reply_port)
     {
-        init_sockaddr_in ((struct sockaddr_in *) &bindaddr, NULL, reply_port);
-        if( bind (insockfd, &bindaddr, sizeof (bindaddr)) ) {
+        init_sockaddr ((struct sockaddr *) &bindaddr, NULL, reply_port, af);
+        if( bind (insockfd, (struct sockaddr *)&bindaddr, sizeof (bindaddr)) ) {
 		fprintf (stderr, "bind failure\n" );
 	}
     }
Index: drivers/netjack/netjack.c
===================================================================
--- drivers/netjack/netjack.c	(revision 4522)
+++ drivers/netjack/netjack.c	(working copy)
@@ -48,6 +48,7 @@
 #else
 #include <sys/socket.h>
 #include <netinet/in.h>
+#include <netdb.h>
 #endif
 
 #include "config.h"
@@ -140,7 +141,7 @@
     // check if we know who to send our packets too.
     if (!netj->srcaddress_valid)
 	if( netj->packcache->master_address_valid ) {
-	    memcpy (&(netj->syncsource_address), &(netj->packcache->master_address), sizeof( struct sockaddr_in ) );
+	    memcpy (&(netj->syncsource_address), &(netj->packcache->master_address), sizeof( struct sockaddr_storage ) );
 	    netj->srcaddress_valid = 1;
 	}
 
@@ -352,11 +353,11 @@
     {
 	int r;
 	if (netj->reply_port)
-	    netj->syncsource_address.sin_port = htons(netj->reply_port);
+	    sockaddr_set_port((struct sockaddr *)&netj->syncsource_address, netj->reply_port);
 
 	for( r=0; r<netj->redundancy; r++ )
 	    netjack_sendto(netj->outsockfd, (char *)packet_buf, tx_size,
-		    0, (struct sockaddr*)&(netj->syncsource_address), sizeof(struct sockaddr_in), netj->mtu);
+		    0, (struct sockaddr*)&(netj->syncsource_address), sizeof(struct sockaddr_storage), netj->mtu);
     }
 }
 
@@ -570,7 +571,8 @@
 		unsigned int redundancy,
 		int dont_htonl_floats,
 		int always_deadline,
-		int jitter_val )
+		int jitter_val,
+		int ipv4_only)
 {
 
     // Fill in netj values.
@@ -582,6 +584,7 @@
     netj->period_size = period_size;
     netj->dont_htonl_floats = dont_htonl_floats;
 
+    netj->ipv4_only = ipv4_only;
     netj->listen_port   = listen_port;
 
     netj->capture_channels  = capture_ports + capture_ports_midi;
@@ -636,10 +639,29 @@
 int
 netjack_startup( netjack_driver_state_t *netj )
 {
-    int first_pack_len;
-    struct sockaddr_in address;
+    int first_pack_len, r;
+    struct addrinfo hints, *res;
+    char port_str[8];
+    
+    memset(&hints, 0, sizeof(hints));
+    if (netj->ipv4_only == TRUE)
+	hints.ai_family = AF_INET;
+    else
+	hints.ai_family = AF_INET6;
+    hints.ai_socktype = SOCK_DGRAM;
+    hints.ai_flags = AI_PASSIVE;     // fill in my IP for me
+    
+    sprintf(port_str, "%u", netj->listen_port);
+    
+    r = getaddrinfo(NULL, port_str, &hints, &res);
+    if (r != 0)
+    {
+        jack_info("address lookup failure: %s", gai_strerror(r));
+	return -1;
+    }
+    
     // Now open the socket, and wait for the first packet to arrive...
-    netj->sockfd = socket (AF_INET, SOCK_DGRAM, 0);
+    netj->sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
 #ifdef WIN32
     if (netj->sockfd == INVALID_SOCKET)
 #else
@@ -649,33 +671,23 @@
         jack_info ("socket error");
         return -1;
     }
-    address.sin_family = AF_INET;
-    address.sin_port = htons(netj->listen_port);
-    address.sin_addr.s_addr = htonl(INADDR_ANY);
-    if (bind (netj->sockfd, (struct sockaddr *) &address, sizeof (address)) < 0)
+    
+    if (bind (netj->sockfd, res->ai_addr, res->ai_addrlen) < 0)
     {
         jack_info("bind error");
         return -1;
     }
-
-    netj->outsockfd = socket (AF_INET, SOCK_DGRAM, 0);
-#ifdef WIN32
-    if (netj->outsockfd == INVALID_SOCKET)
-#else
-    if (netj->outsockfd == -1)
-#endif
-    {
-        jack_info ("socket error");
-        return -1;
-    }
+    
+    freeaddrinfo(res);
+    
     netj->srcaddress_valid = 0;
     if (netj->use_autoconfig)
     {
 	jacknet_packet_header *first_packet = alloca (sizeof (jacknet_packet_header));
 #ifdef WIN32
-    int address_size = sizeof( struct sockaddr_in );
+    int address_size = sizeof( struct sockaddr_storage );
 #else
-	socklen_t address_size = sizeof (struct sockaddr_in);
+    socklen_t address_size = sizeof (struct sockaddr_storage);
 #endif
 	//jack_info ("Waiting for an incoming packet !!!");
 	//jack_info ("*** IMPORTANT *** Dont connect a client to jackd until the driver is attached to a clock source !!!");
@@ -698,6 +710,17 @@
     }
 	netj->srcaddress_valid = 1;
 
+	netj->outsockfd = socket(netj->syncsource_address.ss_family, SOCK_DGRAM, 0);
+#ifdef WIN32
+	if (netj->outsockfd == INVALID_SOCKET)
+#else
+        if (netj->outsockfd == -1)
+#endif
+	{
+	    jack_info ("socket error");
+	    return -1;
+	}
+
 	if (first_pack_len == sizeof (jacknet_packet_header))
 	{
 	    packet_header_ntoh (first_packet);
Index: drivers/netjack/net_driver.c
===================================================================
--- drivers/netjack/net_driver.c	(revision 4522)
+++ drivers/netjack/net_driver.c	(working copy)
@@ -253,11 +253,11 @@
 #endif
 
         if (netj->reply_port)
-            netj->syncsource_address.sin_port = htons(netj->reply_port);
+	    sockaddr_set_port((struct sockaddr *)&netj->syncsource_address, netj->reply_port);
 
 	for( r=0; r<netj->redundancy; r++ )
 	    netjack_sendto(netj->sockfd, (char *)packet_buf, packet_size,
-			   flag, (struct sockaddr*)&(netj->syncsource_address), sizeof(struct sockaddr_in), netj->mtu);
+			   flag, (struct sockaddr*)&(netj->syncsource_address), sizeof(struct sockaddr_storage), netj->mtu);
     }
 
     return 0;
@@ -318,13 +318,14 @@
 		unsigned int redundancy,
 		int dont_htonl_floats,
 		int always_deadline,
-		int jitter_val)
+		int jitter_val,
+                int ipv4_only)
 {
     net_driver_t * driver;
 
     jack_info ("creating net driver ... %s|%" PRIu32 "|%" PRIu32
-            "|%u|%u|%u|transport_sync:%u", name, sample_rate, period_size, listen_port,
-            capture_ports, playback_ports, transport_sync);
+            "|%u|%u|%u|transport_sync:%u|v%u", name, sample_rate, period_size, listen_port,
+            capture_ports, playback_ports, transport_sync, (ipv4_only == TRUE) ? 4 : 6);
 
     driver = (net_driver_t *) calloc (1, sizeof (net_driver_t));
 
@@ -362,7 +363,8 @@
 		redundancy,
 		dont_htonl_floats,
 	        always_deadline, 
-		jitter_val	);
+	        jitter_val,
+	        ipv4_only);
 
     netjack_startup( netj );
 
@@ -387,7 +389,7 @@
 
     desc = calloc (1, sizeof (jack_driver_desc_t));
     strcpy (desc->name, "net");
-    desc->nparams = 18;
+    desc->nparams = 19;
 
     params = calloc (desc->nparams, sizeof (jack_driver_param_desc_t));
 
@@ -547,6 +549,16 @@
     strcpy (params[i].long_desc, params[i].short_desc);
     desc->params = params;
 
+    i++;
+    strcpy (params[i].name, "v4");
+    params[i].character  = '4';
+    params[i].type       = JackDriverParamBool;
+    params[i].value.i    = FALSE;
+    strcpy (params[i].short_desc,
+            "Use only IPv4");
+    strcpy (params[i].long_desc, params[i].short_desc);
+    desc->params = params;
+
     return desc;
 }
 
@@ -572,6 +584,7 @@
     int dont_htonl_floats = 0;
     int always_deadline = 0;
     int jitter_val = 0;
+    int ipv4_only = FALSE;
     const JSList * node;
     const jack_driver_param_t * param;
 
@@ -630,6 +643,10 @@
                 bitdepth = param->value.ui;
                 break;
 
+            case '4':
+	        ipv4_only = param->value.i;
+                break;
+
 	    case 'c':
 #if HAVE_CELT
 		bitdepth = 1000;
@@ -674,7 +691,8 @@
                            listen_port, handle_transport_sync,
                            resample_factor, resample_factor_up, bitdepth,
 			   use_autoconfig, latency, redundancy,
-			   dont_htonl_floats, always_deadline, jitter_val);
+			   dont_htonl_floats, always_deadline, jitter_val,
+	                   ipv4_only);
 }
 
 void
Index: drivers/netjack/netjack.h
===================================================================
--- drivers/netjack/netjack.h	(revision 4522)
+++ drivers/netjack/netjack.h	(working copy)
@@ -58,6 +58,7 @@
 
     jack_nframes_t  codec_latency;
 
+    int             ipv4_only;
     unsigned int    listen_port;
 
     unsigned int    capture_channels;
@@ -82,7 +83,7 @@
     int		    outsockfd;
 #endif
 
-    struct sockaddr_in syncsource_address;
+    struct sockaddr_storage syncsource_address;
 
     int		    reply_port;
     int		    srcaddress_valid;
@@ -144,7 +145,8 @@
 		unsigned int redundancy,
 		int dont_htonl_floats,
 		int always_deadline,
-		int jitter_val );
+		int jitter_val,
+ 	        int ipv4_only );
 
 void netjack_release( netjack_driver_state_t *netj );
 int netjack_startup( netjack_driver_state_t *netj );
Index: drivers/netjack/netjack_packet.c
===================================================================
--- drivers/netjack/netjack_packet.c	(revision 4522)
+++ drivers/netjack/netjack_packet.c	(working copy)
@@ -527,13 +527,13 @@
     int rcv_len;
     jack_nframes_t framecnt;
     cache_packet *cpack;
-    struct sockaddr_in sender_address;
+    struct sockaddr_storage sender_address;
 #ifdef WIN32
-    size_t senderlen = sizeof( struct sockaddr_in );
+    size_t senderlen = sizeof( struct sockaddr_storage );
     u_long parm = 1;
     ioctlsocket( sockfd, FIONBIO, &parm );
 #else
-    socklen_t senderlen = sizeof( struct sockaddr_in );
+    socklen_t senderlen = sizeof( struct sockaddr_storage );
 #endif
     while (1)
     {
@@ -1475,3 +1475,19 @@
     else
         render_jack_ports_to_payload_float (playback_ports, playback_srcs, nframes, packet_payload, net_period_up, dont_htonl_floats);
 }
+
+
+void
+sockaddr_set_port(struct sockaddr *addr, int port)
+{
+    uint16_t nport = htons(port);
+    switch (addr->sa_family)
+    {
+        case AF_INET:
+	    ((struct sockaddr_in *)addr)->sin_port = nport;
+	    break;
+        case AF_INET6:
+	    ((struct sockaddr_in6 *)addr)->sin6_port = nport;
+	    break;
+    }
+}
Index: drivers/netjack/netjack_packet.h
===================================================================
--- drivers/netjack/netjack_packet.h	(revision 4522)
+++ drivers/netjack/netjack_packet.h	(working copy)
@@ -101,7 +101,7 @@
     int size;
     cache_packet *packets;
     int mtu;
-    struct sockaddr_in master_address;
+    struct sockaddr_storage master_address;
     int master_address_valid;
     jack_nframes_t last_framecnt_retreived;
     int last_framecnt_retreived_valid;
@@ -152,6 +152,11 @@
 
 void decode_midi_buffer (uint32_t *buffer_uint32, unsigned int buffer_size_uint32, jack_default_audio_sample_t* buf);
 void encode_midi_buffer (uint32_t *buffer_uint32, unsigned int buffer_size_uint32, jack_default_audio_sample_t* buf);
+
+/** Set the port field for a socket address */
+void sockaddr_set_port(struct sockaddr *addr, int port);
+
+
 #ifdef __cplusplus
     }
 #endif
