Index: src/mod/endpoints/mod_sofia/sofia_reg.c
===================================================================
--- src/mod/endpoints/mod_sofia/sofia_reg.c	(revision 10565)
+++ src/mod/endpoints/mod_sofia/sofia_reg.c	(working copy)
@@ -351,7 +351,7 @@
 	char to[128] = "";
 
 	switch_snprintf(to, sizeof(to), "sip:%s@%s", argv[1], argv[2]);
-	contact = sofia_glue_get_url_from_contact(argv[3], 1);
+	contact = sofia_glue_get_url_from_contact(argv[3], 1, 0);
 
 	nh = nua_handle(profile->nua, NULL, SIPTAG_FROM_STR(profile->url), SIPTAG_TO_STR(to), NUTAG_URL(contact), SIPTAG_CONTACT_STR(profile->url), TAG_END());
 	nua_handle_bind(nh, &mod_sofia_globals.destroy_private);
@@ -389,7 +389,7 @@
 		event = "reboot_now";
 	}
 
-	if ((contact_url = sofia_glue_get_url_from_contact((char *)contact, 1))) {
+	if ((contact_url = sofia_glue_get_url_from_contact((char *)contact, 1, 0))) {
 		char *p;
 		id = switch_mprintf("sip:%s@%s", user, host);
 
@@ -983,7 +983,7 @@
 	} else {
 		if (multi_reg) {
 			char *icontact, *p;
-			icontact = sofia_glue_get_url_from_contact(contact_str, 1);
+			icontact = sofia_glue_get_url_from_contact(contact_str, 1, 0);
 			if ((p = strchr(icontact, ';'))) {
 				*p = '\0';
 			}
Index: src/mod/endpoints/mod_sofia/mod_sofia.c
===================================================================
--- src/mod/endpoints/mod_sofia/mod_sofia.c	(revision 10565)
+++ src/mod/endpoints/mod_sofia/mod_sofia.c	(working copy)
@@ -1811,8 +1811,8 @@
 	struct cb_helper *cb = (struct cb_helper *) pArg;
 	char *contact;
 
-	if (!switch_strlen_zero(argv[0]) && (contact = sofia_glue_get_url_from_contact(argv[0], 1)) ) {
-		cb->stream->write_function(cb->stream, "%ssofia/%s/sip:%s,", argv[2], argv[1], sofia_glue_strip_proto(contact));
+	if (!switch_strlen_zero(argv[0]) && (contact = sofia_glue_get_url_from_contact(argv[0], 1, 1)) ) {
+		cb->stream->write_function(cb->stream, "%ssofia/%s/sip:%s,", argv[2], argv[1], contact);
 		free(contact);
 	}
 
@@ -2328,7 +2328,7 @@
 
 	id = switch_mprintf("sip:%s@%s", user, host);
 	switch_assert(id);
-	contact = sofia_glue_get_url_from_contact(contact_in, 1);
+	contact = sofia_glue_get_url_from_contact(contact_in, 1, 0);
 				
 	if ((p = strstr(contact, ";fs_"))) {
 		*p = '\0';
@@ -2428,7 +2428,7 @@
 				id = switch_mprintf("sip:%s@%s", user, host);
 
 				switch_assert(id);
-				contact = sofia_glue_get_url_from_contact(buf, 0);
+				contact = sofia_glue_get_url_from_contact(buf, 0, 0);
 
 				if ((p = strstr(contact, ";fs_"))) {
 					*p = '\0';
Index: src/mod/endpoints/mod_sofia/mod_sofia.h
===================================================================
--- src/mod/endpoints/mod_sofia/mod_sofia.h	(revision 10565)
+++ src/mod/endpoints/mod_sofia/mod_sofia.h	(working copy)
@@ -265,10 +265,15 @@
 
 typedef enum {
 	SOFIA_TRANSPORT_UNKNOWN = 0,
-	SOFIA_TRANSPORT_UDP,
-	SOFIA_TRANSPORT_TCP,
-	SOFIA_TRANSPORT_TCP_TLS,
-	SOFIA_TRANSPORT_SCTP
+	SOFIA_TRANSPORT_UDP	= (1 << 0),
+	SOFIA_TRANSPORT_TCP	= (1 << 1),
+	SOFIA_TRANSPORT_SCTP	= (1 << 2),
+
+	SOFIA_TRANSPORT_SECURE	= (1 << 16),
+	SOFIA_TRANSPORT_USE_SIPS= (1 << 17),
+
+	SOFIA_TRANSPORT_TCP_TLS = (SOFIA_TRANSPORT_TCP | SOFIA_TRANSPORT_SECURE),
+	SOFIA_TRANSPORT_SCTP_TLS= (SOFIA_TRANSPORT_SCTP | SOFIA_TRANSPORT_SECURE | SOFIA_TRANSPORT_USE_SIPS),
 } sofia_transport_t;
 
 typedef enum {
@@ -668,7 +673,7 @@
 extern switch_endpoint_interface_t *sofia_endpoint_interface;
 void sofia_presence_set_chat_hash(private_object_t *tech_pvt, sip_t const *sip);
 switch_status_t sofia_on_hangup(switch_core_session_t *session);
-char *sofia_glue_get_url_from_contact(char *buf, uint8_t to_dup);
+char *sofia_glue_get_url_from_contact(char *buf, const uint8_t to_dup, const uint8_t strip_proto);
 void sofia_presence_set_hash_key(char *hash_key, int32_t len, sip_t const *sip);
 void sofia_glue_sql_close(sofia_profile_t *profile);
 int sofia_glue_init_sql(sofia_profile_t *profile);
@@ -736,6 +741,7 @@
 sofia_transport_t sofia_glue_str2transport(const char *str);
 
 const char *sofia_glue_transport2str(const sofia_transport_t tp);
+const char *sofia_glue_transport2scheme(const sofia_transport_t tp);
 char * sofia_glue_find_parameter(const char *str, const char *param);
 
 int sofia_glue_transport_has_tls(const sofia_transport_t tp);
Index: src/mod/endpoints/mod_sofia/sofia_presence.c
===================================================================
--- src/mod/endpoints/mod_sofia/sofia_presence.c	(revision 10565)
+++ src/mod/endpoints/mod_sofia/sofia_presence.c	(working copy)
@@ -120,7 +120,7 @@
 	}
 
 	status = SWITCH_STATUS_SUCCESS;
-	contact = sofia_glue_get_url_from_contact(buf, 1);
+	contact = sofia_glue_get_url_from_contact(buf, 1, 0);
 	/* if this cries, add contact here too, change the 1 to 0 and omit the safe_free */
 	msg_nh = nua_handle(profile->nua, NULL, SIPTAG_FROM_STR(from), NUTAG_URL(contact), SIPTAG_TO_STR(buf),	
 						SIPTAG_CONTACT_STR(profile->url), TAG_END());
@@ -1242,7 +1242,7 @@
 
 	id = switch_mprintf("sip:%s@%s", sub_to_user, sub_to_host);
 
-	contact = sofia_glue_get_url_from_contact(o_contact, 1);
+	contact = sofia_glue_get_url_from_contact(o_contact, 1, 0);
 	if ((route = strstr(contact, ";fs_path=")) && (route = strdup(route + 9))) {
 		
 		for (p = route; p && *p ; p++) {
Index: src/mod/endpoints/mod_sofia/sofia_glue.c
===================================================================
--- src/mod/endpoints/mod_sofia/sofia_glue.c	(revision 10565)
+++ src/mod/endpoints/mod_sofia/sofia_glue.c	(working copy)
@@ -660,47 +660,85 @@
 	return NULL;
 }
 
+static sofia_transport_t sofia_glue_make_secure(const sofia_transport_t tp, const int sips)
+{
+	sofia_transport_t val = tp;
+
+	if (val & SOFIA_TRANSPORT_SECURE)
+		return val;
+
+	switch (val) {
+	case SOFIA_TRANSPORT_TCP:
+	case SOFIA_TRANSPORT_SCTP:
+		val |= SOFIA_TRANSPORT_SECURE;
+	default:
+		return SOFIA_TRANSPORT_UNKNOWN;
+	}
+
+	if (sips)
+		val |= SOFIA_TRANSPORT_USE_SIPS;
+
+	return val;
+}
+
 sofia_transport_t sofia_glue_url2transport(const url_t *url)
 {
+	sofia_transport_t tp = SOFIA_TRANSPORT_UNKNOWN;
 	char *ptr = NULL;
-	int tls = 0;
+	int sips = 0, security = 0;
 
 	if (!url)
 		return SOFIA_TRANSPORT_UNKNOWN;
 
 	if (url->url_scheme && !strcasecmp(url->url_scheme, "sips")) {
-		tls++;
+		sips = 1;
 	}
 
 	if ((ptr = sofia_glue_find_parameter(url->url_params, "transport="))) {
-		return sofia_glue_str2transport(ptr + 10);
+		tp = sofia_glue_str2transport(ptr + 10);
 	}
 
-	return (tls) ? SOFIA_TRANSPORT_TCP_TLS : SOFIA_TRANSPORT_UDP;
+	if ((ptr = sofia_glue_find_parameter(url->url_params, "security="))) {
+		if (!strcasecmp(ptr + 9, "on")) {
+			security = 1;
+		}
+	}
+
+	return (sips || security) ? sofia_glue_make_secure(tp, sips) : tp;
 }
 
 sofia_transport_t sofia_glue_via2transport(const sip_via_t *via)
 {
+	sofia_transport_t tp = SOFIA_TRANSPORT_UNKNOWN;
 	char *ptr = NULL;
+	int sips = 0;
 
 	if (!via || !via->v_protocol)
 		return SOFIA_TRANSPORT_UNKNOWN;
 
+	/* SIPS? */
+	if (!strcasecmp(via->v_protocol, "sips")) {
+		sips = 1;
+	}
+
 	if ((ptr = strrchr(via->v_protocol, '/'))) {
 		ptr++;
 
 		if (!strncasecmp(ptr, "udp", 3)) {
-			return SOFIA_TRANSPORT_UDP;
-		} else if (!strncasecmp(ptr, "tcp", 3)) {
-			return SOFIA_TRANSPORT_TCP;
-		} else if (!strncasecmp(ptr, "tls", 3)) {
-			return SOFIA_TRANSPORT_TCP_TLS;
-		} else if (!strncasecmp(ptr, "sctp", 4)) {
-			return SOFIA_TRANSPORT_SCTP;
+			tp = SOFIA_TRANSPORT_UDP;
 		}
+		else if (!strncasecmp(ptr, "tcp", 3)) {
+			tp = SOFIA_TRANSPORT_TCP;
+		}
+		else if (!strncasecmp(ptr, "tls", 3)) {
+			tp = SOFIA_TRANSPORT_TCP_TLS;
+		}
+		else if (!strncasecmp(ptr, "sctp", 4)) {
+			tp = SOFIA_TRANSPORT_SCTP;
+		}
 	}
 
-	return SOFIA_TRANSPORT_UNKNOWN;
+	return (sips) ? sofia_glue_make_secure(tp, sips) : tp;
 }
 
 const char *sofia_glue_transport2str(const sofia_transport_t tp)
@@ -710,9 +748,10 @@
 		return "tcp";
 
 	case SOFIA_TRANSPORT_TCP_TLS:
-		return "tls";
+		return (tp & SOFIA_TRANSPORT_USE_SIPS) ? "tcp" : "tls";
 
 	case SOFIA_TRANSPORT_SCTP:
+	case SOFIA_TRANSPORT_SCTP_TLS:
 		return "sctp";
 
 	default:
@@ -720,17 +759,30 @@
 	}
 }
 
+const char *sofia_glue_transport2scheme(const sofia_transport_t tp)
+{
+	if (tp & SOFIA_TRANSPORT_USE_SIPS)
+		return "sips";
+
+	return "sip";
+}
+
 int sofia_glue_transport_has_tls(const sofia_transport_t tp)
 {
-	switch (tp) {
-	case SOFIA_TRANSPORT_TCP_TLS:
+	if (tp & SOFIA_TRANSPORT_SECURE)
 		return 1;
 
-	default:
-		return 0;
-	}
+	return 0;
 }
 
+int sofia_glue_transport_is_sips(const sofia_transport_t tp)
+{
+	if (tp & SOFIA_TRANSPORT_USE_SIPS)
+		return 1;
+
+	return 0;
+}
+
 char *sofia_overcome_sip_uri_weakness(switch_core_session_t *session, const char *uri, const sofia_transport_t transport, switch_bool_t uri_only,
 									  const char *params)
 {
@@ -738,7 +790,7 @@
 	char *new_uri = NULL;
 	char *p;
 
-	stripped = sofia_glue_get_url_from_contact(stripped, 0);
+	stripped = sofia_glue_get_url_from_contact(stripped, 0, 1);
 
 	/* remove our params so we don't make any whiny moronic device piss it's pants and forget who it is for a half-hour */
 	if ((p = (char *)switch_stristr(";fs_", stripped))) {
@@ -748,30 +800,30 @@
 	if (transport && transport != SOFIA_TRANSPORT_UDP) {
 
 		if (switch_stristr("port=", stripped)) {
-			new_uri = switch_core_session_sprintf(session, "%s%s%s", uri_only ? "" : "<", stripped, uri_only ? "" : ">");
+			new_uri = switch_core_session_sprintf(session, "%s%s%s%s", uri_only ? "" : "<", sofia_glue_transport2scheme(transport), stripped, uri_only ? "" : ">");
 		} else {
 
 			if (strchr(stripped, ';')) {
 				if (params) {
-					new_uri = switch_core_session_sprintf(session, "%s%s&transport=%s;%s%s",
-														  uri_only ? "" : "<", stripped, sofia_glue_transport2str(transport), params, uri_only ? "" : ">");
+					new_uri = switch_core_session_sprintf(session, "%s%s%s&transport=%s;%s%s",
+														  uri_only ? "" : "<", sofia_glue_transport2scheme(transport), stripped, sofia_glue_transport2str(transport), params, uri_only ? "" : ">");
 				} else {
-					new_uri = switch_core_session_sprintf(session, "%s%s&transport=%s%s",
-														  uri_only ? "" : "<", stripped, sofia_glue_transport2str(transport), uri_only ? "" : ">");
+					new_uri = switch_core_session_sprintf(session, "%s%s%s&transport=%s%s",
+														  uri_only ? "" : "<", sofia_glue_transport2scheme(transport), stripped, sofia_glue_transport2str(transport), uri_only ? "" : ">");
 				}
 			} else {
 				if (params) {
-					new_uri = switch_core_session_sprintf(session, "%s%s;transport=%s;%s%s",
-														  uri_only ? "" : "<", stripped, sofia_glue_transport2str(transport), params, uri_only ? "" : ">");
+					new_uri = switch_core_session_sprintf(session, "%s%s%s;transport=%s;%s%s",
+														  uri_only ? "" : "<", sofia_glue_transport2scheme(transport), stripped, sofia_glue_transport2str(transport), params, uri_only ? "" : ">");
 				} else {
-					new_uri = switch_core_session_sprintf(session, "%s%s;transport=%s%s",
-														  uri_only ? "" : "<", stripped, sofia_glue_transport2str(transport), uri_only ? "" : ">");
+					new_uri = switch_core_session_sprintf(session, "%s%s%s;transport=%s%s",
+														  uri_only ? "" : "<", sofia_glue_transport2scheme(transport), stripped, sofia_glue_transport2str(transport), uri_only ? "" : ">");
 				}
 			}
 		}
 	} else {
 		if (params) {
-			new_uri = switch_core_session_sprintf(session, "%s%s;%s%s", uri_only ? "" : "<", stripped, params, uri_only ? "" : ">");
+			new_uri = switch_core_session_sprintf(session, "%ssip:%s;%s%s", uri_only ? "" : "<", stripped, params, uri_only ? "" : ">");
 		} else {
 			if (uri_only) {
 				new_uri = stripped;
@@ -1098,7 +1150,7 @@
 			return SWITCH_STATUS_FALSE;
 		}
 
-		if ((d_url = sofia_glue_get_url_from_contact(tech_pvt->dest, 1))) {
+		if ((d_url = sofia_glue_get_url_from_contact(tech_pvt->dest, 1, 0))) {
 			url = d_url;
 		} else {
 			url = tech_pvt->dest;
@@ -1119,7 +1171,7 @@
 		}
 
 		rpid_domain = switch_core_session_strdup(session, use_from_str);
-		sofia_glue_get_url_from_contact(rpid_domain, 0);
+		sofia_glue_get_url_from_contact(rpid_domain, 0, 0);
 		if ((rpid_domain = strrchr(rpid_domain, '@'))) {
 			rpid_domain++;
 			if ((p = strchr(rpid_domain, ';'))) {
@@ -2636,12 +2688,17 @@
 	}
 }
 
-char *sofia_glue_get_url_from_contact(char *buf, uint8_t to_dup)
+char *sofia_glue_get_url_from_contact(char *buf, const uint8_t to_dup, const uint8_t strip_proto)
 {
-	char *url = NULL, *e;
+	char *url = NULL, *e, *p;
 
 	if ((url = strchr(buf, '<')) && (e = strchr(url, '>'))) {
 		url++;
+
+		if (strip_proto && (p = strchr(buf, ':'))) {
+			url = ++p;
+		}
+
 		if (to_dup) {
 			url = strdup(url);
 			e = strchr(url, '>');
@@ -2649,12 +2706,17 @@
 
 		*e = '\0';
 	} else {
+		if (strip_proto && (p = strchr(buf, ':'))) {
+			buf = ++p;
+		}
+
 		if (to_dup) {
 			url = strdup(buf);
 		} else {
 			url = buf;
 		}
 	}
+
 	return url;
 }
 
