Index: src/mod/endpoints/mod_sofia/mod_sofia.h
===================================================================
--- src/mod/endpoints/mod_sofia/mod_sofia.h	(revision 6480)
+++ src/mod/endpoints/mod_sofia/mod_sofia.h	(working copy)
@@ -428,8 +428,14 @@
 auth_res_t sofia_reg_parse_auth(sofia_profile_t *profile, sip_authorization_t const *authorization, 
 								const char *regstr, char *np, size_t nplen, char *ip, switch_event_t **v_event, long exptime);
 void sofia_reg_handle_sip_r_challenge(int status,
-					 char const *phrase,
-					 nua_t *nua, sofia_profile_t *profile, nua_handle_t *nh, switch_core_session_t *session, sip_t const *sip, tagi_t tags[]);
+									  char const *phrase,
+									  nua_t *nua,
+									  sofia_profile_t *profile,
+									  nua_handle_t *nh,
+									  switch_core_session_t *session,
+									  sofia_private_t *sofia_private,
+									  sip_t const *sip,
+									  tagi_t tags[]);
 void sofia_reg_handle_sip_r_register(int status,
 					char const *phrase,
 					nua_t *nua, sofia_profile_t *profile, nua_handle_t *nh, sofia_private_t *sofia_private, sip_t const *sip, tagi_t tags[]);
Index: src/mod/endpoints/mod_sofia/sofia.c
===================================================================
--- src/mod/endpoints/mod_sofia/sofia.c	(revision 6480)
+++ src/mod/endpoints/mod_sofia/sofia.c	(working copy)
@@ -143,7 +143,7 @@
 	}
 
 	if (sip && (status == 401 || status == 407)) {
-		sofia_reg_handle_sip_r_challenge(status, phrase, nua, profile, nh, session, sip, tags);
+		sofia_reg_handle_sip_r_challenge(status, phrase, nua, profile, nh, session, sofia_private, sip, tags);
 		goto done;
 	}
 
@@ -548,6 +548,7 @@
 				*register_proxy = NULL,
 				*contact_params = NULL,
 				*params = NULL,
+				*from_key = NULL,
 				*register_transport = "udp";
 			
 			gateway->pool = profile->pool;
@@ -663,6 +664,7 @@
 			
 			gateway->register_url = switch_core_sprintf(gateway->pool, "sip:%s;transport=%s", register_proxy,register_transport);
 			gateway->register_from = switch_core_sprintf(gateway->pool, "<sip:%s@%s;transport=%s>", from_user, from_domain, register_transport);
+			from_key = switch_core_sprintf(gateway->pool, "sip:%s@%s", from_user, from_domain);
 			gateway->register_contact = switch_core_sprintf(gateway->pool, "<sip:%s@%s:%d%s>", extension,
 															profile->extsipip ? profile->extsipip : profile->sipip, profile->sip_port, params);
 
@@ -696,7 +698,7 @@
 				gateway->next = profile->gateways;
 				profile->gateways = gateway;
 				sofia_reg_add_gateway(gateway->name, gateway);
-				sofia_reg_add_gateway(gateway->register_from, gateway);
+				sofia_reg_add_gateway(from_key, gateway);
 				sofia_reg_add_gateway(gateway->register_contact, gateway);
 			}
 		}
Index: src/mod/endpoints/mod_sofia/sofia_reg.c
===================================================================
--- src/mod/endpoints/mod_sofia/sofia_reg.c	(revision 6480)
+++ src/mod/endpoints/mod_sofia/sofia_reg.c	(working copy)
@@ -661,7 +661,13 @@
 
 void sofia_reg_handle_sip_r_challenge(int status,
 							char const *phrase,
-							nua_t * nua, sofia_profile_t *profile, nua_handle_t * nh, switch_core_session_t *session, sip_t const *sip, tagi_t tags[])
+							nua_t * nua,
+							sofia_profile_t *profile,
+							nua_handle_t * nh,
+							switch_core_session_t *session,
+							sofia_private_t *sofia_private,
+							sip_t const *sip,
+							tagi_t tags[])
 {
 	sofia_gateway_t *gateway = NULL;
 	sip_www_authenticate_t const *authenticate = NULL;
@@ -707,8 +713,16 @@
 
 	if (profile) {
 		sofia_gateway_t *gateway_ptr = NULL;
+		if (sofia_private && sofia_private->gateway) {
+			gateway_ptr = sofia_private->gateway;
+		}
 
-		if ((duprealm = strdup(realm))) {
+		if (!gateway) {
+			if (!(duprealm = strdup(realm))) {
+				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Memory Error!\n");
+				goto cancel;
+			}
+
 			qrealm = duprealm;
 
 			while (*qrealm && *qrealm == '"') {
@@ -730,43 +744,40 @@
 
 				switch_safe_free(from_key);
 			}
+		}
 
-			if (!gateway) {
-				switch_mutex_lock(mod_sofia_globals.hash_mutex);
-				for (gateway_ptr = profile->gateways; gateway_ptr; gateway_ptr = gateway_ptr->next) {
-					if (scheme && qrealm && !strcasecmp(gateway_ptr->register_scheme, scheme)
-						&& !strcasecmp(gateway_ptr->register_realm, qrealm)) {
-						gateway = gateway_ptr;
+		if (!gateway) {
+			switch_mutex_lock(mod_sofia_globals.hash_mutex);
+			for (gateway_ptr = profile->gateways; gateway_ptr; gateway_ptr = gateway_ptr->next) {
+				if (scheme && qrealm && !strcasecmp(gateway_ptr->register_scheme, scheme)
+					&& !strcasecmp(gateway_ptr->register_realm, qrealm)) {
+					gateway = gateway_ptr;
 
-						if (switch_thread_rwlock_tryrdlock(gateway->profile->rwlock) != SWITCH_STATUS_SUCCESS) {
+					if (switch_thread_rwlock_tryrdlock(gateway->profile->rwlock) != SWITCH_STATUS_SUCCESS) {
 #ifdef SOFIA_DEBUG_RWLOCKS
-							switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Profile %s is locked\n", gateway->profile->name);
+						switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Profile %s is locked\n", gateway->profile->name);
 #endif
-							gateway = NULL;
-						}
+						gateway = NULL;
+					}
 #ifdef SOFIA_DEBUG_RWLOCKS
-						switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "XXXXXXXXXXXXXX GW LOCK %s\n", gateway->profile->name);
+					switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "XXXXXXXXXXXXXX GW LOCK %s\n", gateway->profile->name);
 #endif
-						break;
-					}
+					break;
 				}
-				switch_mutex_unlock(mod_sofia_globals.hash_mutex);
 			}
-			
-			if (!gateway) {
-				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "No Match for Scheme [%s] Realm [%s]\n", scheme, qrealm);
-			}
-			
-			switch_safe_free(duprealm);
+			switch_mutex_unlock(mod_sofia_globals.hash_mutex);
+		}
+		
+		if (!gateway) {
+			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "No Match for Scheme [%s] Realm [%s]\n", scheme, qrealm);
+		}
+		
+		switch_safe_free(duprealm);
 
-			if (!gateway) {
-				goto cancel;
-			}
-
-		} else {
-			switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Memory Error!\n");
+		if (!gateway) {
 			goto cancel;
 		}
+
 	}
 
 	snprintf(authentication, sizeof(authentication), "%s:%s:%s:%s", scheme, realm, gateway->register_username, gateway->register_password);
