Index: libeXosip2/src/eXconf.c
===================================================================
--- libeXosip2/src/eXconf.c	(revision 389)
+++ libeXosip2/src/eXconf.c	(revision 416)
@@ -636,4 +636,5 @@
 	int i;
 	struct eXtl_protocol* proto;
+	int port;
 
 	for(i = 0; proto = socks_proto_tab[i]; i++)
@@ -650,6 +651,7 @@
 
 	eXosip.eXtl = proto;
+	port = atoi(tport);
 	proto->tl_init();
-	proto->tl_masquerade_contact(tserver, atoi(tport));
+	proto->tl_masquerade_contact(tserver, port);
 	snprintf(eXosip.transport, sizeof(eXosip.transport), "%s",
 			proto->proto_num == IPPROTO_UDP ? "UDP" : "TCP");
@@ -666,5 +668,4 @@
 	}
 #endif
-
 	return 0;
 }
@@ -770,11 +771,13 @@
 	eXosip.keep_alive = 17000;
 
-	eXtl_udp.tl_init();
-	eXtl_tcp.tl_init();
+	if (!eXosip.eXtl) {
+		eXtl_udp.tl_init();
+		eXtl_tcp.tl_init();
 #ifdef HAVE_OPENSSL_SSL_H
 #if !(OPENSSL_VERSION_NUMBER < 0x00908000L)
 	eXtl_dtls.tl_init();
 #endif
-	eXtl_tls.tl_init();
+		eXtl_tls.tl_init();
+	}
 #endif
 	return OSIP_SUCCESS;
Index: libeXosip2/src/eXtl_socks.c
===================================================================
--- libeXosip2/src/eXtl_socks.c	(revision 390)
+++ libeXosip2/src/eXtl_socks.c	(revision 416)
@@ -284,5 +284,5 @@
 		return -1;
 
-	if (resp.atp != 0)
+	if (resp.atp != 1)
 		return -1;
 
@@ -293,5 +293,5 @@
 	ina.s_addr = resp.destip;
 	osip_strncpy(sk->public_ip, inet_ntoa(ina), sizeof(sk->public_ip)-1);
-	sk->public_port = ntohl(resp.dport);
+	sk->public_port = ntohs(resp.dport);
 
 	return 0;
@@ -334,11 +334,10 @@
 				curinfo->ai_protocol);
 
-
-		if (sock < 0) {
-			OSIP_TRACE(osip_trace
-					(__FILE__, __LINE__, OSIP_ERROR, NULL,
-							"Cannot create socket %s!\n", strerror(ex_errno)));
-			continue;
-		}
+		if (sock)
+			break;
+
+		OSIP_TRACE(osip_trace
+				(__FILE__, __LINE__, OSIP_ERROR, NULL,
+						"Cannot create socket %s!\n", strerror(ex_errno)));
 
 	}
@@ -348,5 +347,5 @@
 		sk->socket = sock;
 		sk->remote_port = port;
-		osip_strncpy(sk->remote_ip, addr, sizeof(sk->remote_ip));
+		osip_strncpy(sk->remote_ip, addr, sizeof(sk->remote_ip)-1);
 		if (!socks_negotiate(sk)) {
 			eXosip_freeaddrinfo(addrinfo);
@@ -375,5 +374,5 @@
 				if (mode & SOCKST_UDPMASK) {
 					sk->pbuf = osip_malloc(SIP_MESSAGE_MAX_LENGTH);
-					if (sk->pbuf) {
+					if (!sk->pbuf) {
 						closesocket(sk->socket);
 						return 0;
@@ -852,4 +851,5 @@
 			return -1;
 		memcpy(pfx+1, message, length);
+		pfx->len = htonl(length);
 		length += sizeof(*pfx);
 		osip_free(message);
Index: libeXosip2/src/jcallback.c
===================================================================
--- libeXosip2/src/jcallback.c	(revision 268)
+++ libeXosip2/src/jcallback.c	(revision 416)
@@ -193,5 +193,8 @@
 
 	i = -1;
-	if (osip_strcasecmp(via->protocol, "udp") == 0) {
+	if (eXosip.eXtl) {
+		i = eXosip.eXtl->tl_send_message(tr, sip, host, port, out_socket);
+	}
+	else if (osip_strcasecmp(via->protocol, "udp") == 0) {
 		i = eXtl_udp.tl_send_message(tr, sip, host, port, out_socket);
 	} else if (osip_strcasecmp(via->protocol, "tcp") == 0) {
Index: phapi/phapi.c
===================================================================
--- phapi/phapi.c	(revision 415)
+++ phapi/phapi.c	(revision 416)
@@ -290,9 +290,9 @@
 		eos++;
 
-	while(*uri && !isspace(*uri))
+	while(*uri && isspace(*uri))
 		uri++;
 
 
-	copyfield(tun->tunnel_proto,  uri, addr-1, sizeof(tun->tunnel_proto)-1);
+	copyfield(tun->tunnel_proto,  uri, addr, sizeof(tun->tunnel_proto)-1);
 	copyfield(tun->tunnel_server,  addr+3, port, sizeof(tun->tunnel_server)-1);
 	copyfield(tun->tunnel_port,  port+1, eos, sizeof(tun->tunnel_port)-1);
@@ -320,5 +320,5 @@
 	}
 
-	return tun - tinfo;
+	return i;
 }
 
@@ -4262,9 +4262,5 @@
 	}
 
-	if (phcfg.use_tunnel & PH_TUNNEL_USE) {
-		tun = ph_select_tunnel(sip_tunnel_set, TUNNEL_MAX);
-		if (tun)
-			eXosip_set_tunnel(tun->tunnel_proto, tun->tunnel_server, tun->tunnel_port);;
-	}
+	return 0;
 #endif
 
@@ -4728,4 +4724,13 @@
 	if (i)
 		return -1;
+
+	if (phcfg.use_tunnel & PH_TUNNEL_USE) {
+		struct tunnel_info *tun;
+
+		tun = ph_select_tunnel(sip_tunnel_set, sip_tunnel_count);
+		if (tun)
+			eXosip_set_tunnel(tun->tunnel_proto, tun->tunnel_server, tun->tunnel_port);;
+	}
+
 	
 	ph_nat_init();
@@ -4739,7 +4744,9 @@
 	}
 
-	i = phListenAddr(atoi(phcfg.sipport));
-	if(i)
-		return -1;
+	if (!(phcfg.use_tunnel & PH_TUNNEL_USE)) {
+		i = phListenAddr(atoi(phcfg.sipport));
+		if(i)
+			return -1;
+	}
 
 	{
