Index: /pjproject/trunk/pjlib/src/pj/config.c
===================================================================
--- /pjproject/trunk/pjlib/src/pj/config.c	(revision 596)
+++ /pjproject/trunk/pjlib/src/pj/config.c	(revision 597)
@@ -22,5 +22,5 @@
 
 static const char *id = "config.c";
-const char *PJ_VERSION = "0.5.6.1";
+const char *PJ_VERSION = "0.5.6.5";
 
 PJ_DEF(void) pj_dump_config(void)
Index: /pjproject/trunk/pjsip-apps/src/pjsua/pjsua_app.c
===================================================================
--- /pjproject/trunk/pjsip-apps/src/pjsua/pjsua_app.c	(revision 596)
+++ /pjproject/trunk/pjsip-apps/src/pjsua/pjsua_app.c	(revision 597)
@@ -468,5 +468,5 @@
 		return PJ_EINVAL;
 	    }
-	    cur_acc->contact = pj_str(pj_optarg);
+	    cur_acc->force_contact = pj_str(pj_optarg);
 	    break;
 
@@ -715,8 +715,8 @@
 
     /* Contact */
-    if (acc_cfg->contact.slen) {
+    if (acc_cfg->force_contact.slen) {
 	pj_ansi_sprintf(line, "--contact %.*s\n", 
-			(int)acc_cfg->contact.slen, 
-			acc_cfg->contact.ptr);
+			(int)acc_cfg->force_contact.slen, 
+			acc_cfg->force_contact.ptr);
 	pj_strcat2(result, line);
     }
Index: /pjproject/trunk/pjsip/include/pjsua-lib/pjsua.h
===================================================================
--- /pjproject/trunk/pjsip/include/pjsua-lib/pjsua.h	(revision 596)
+++ /pjproject/trunk/pjsip/include/pjsua-lib/pjsua.h	(revision 597)
@@ -1023,5 +1023,5 @@
      * automatically based on the transport address.
      */
-    pj_str_t	    contact;
+    pj_str_t	    force_contact;
 
     /**
Index: /pjproject/trunk/pjsip/include/pjsua-lib/pjsua_internal.h
===================================================================
--- /pjproject/trunk/pjsip/include/pjsua-lib/pjsua_internal.h	(revision 596)
+++ /pjproject/trunk/pjsip/include/pjsua-lib/pjsua_internal.h	(revision 597)
@@ -81,6 +81,8 @@
     int		     index;	    /**< Index in accounts array.	*/
     pj_str_t	     user_part;	    /**< User part of local URI.	*/
-    pj_str_t	     host_part;	    /**< Host part of local URI.	*/
     pj_str_t	     real_contact;  /**< Real contact address.		*/
+
+    pj_str_t	     srv_domain;    /**< Host part of reg server.	*/
+    int		     srv_port;	    /**< Port number of reg server.	*/
 
     pjsip_regc	    *regc;	    /**< Client registration session.   */
Index: /pjproject/trunk/pjsip/src/pjsua-lib/pjsua_acc.c
===================================================================
--- /pjproject/trunk/pjsip/src/pjsua-lib/pjsua_acc.c	(revision 596)
+++ /pjproject/trunk/pjsip/src/pjsua-lib/pjsua_acc.c	(revision 597)
@@ -56,5 +56,5 @@
     pj_strdup_with_null(pool, &dst->id, &src->id);
     pj_strdup_with_null(pool, &dst->reg_uri, &src->reg_uri);
-    pj_strdup_with_null(pool, &dst->contact, &src->contact);
+    pj_strdup_with_null(pool, &dst->force_contact, &src->force_contact);
 
     dst->proxy_cnt = src->proxy_cnt;
@@ -104,5 +104,5 @@
     pjsua_acc *acc = &pjsua_var.acc[acc_id];
     pjsip_uri *uri;
-    pjsip_sip_uri *sip_uri;
+    pjsip_sip_uri *sip_uri, *sip_reg_uri;
     unsigned i;
 
@@ -129,6 +129,34 @@
 
     /* Get the SIP URI object: */
-
     sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
+
+
+    /* Parse registrar URI, if any */
+    if (acc_cfg->reg_uri.slen) {
+	pjsip_uri *reg_uri;
+
+	reg_uri = pjsip_parse_uri(pjsua_var.pool, acc_cfg->reg_uri.ptr,
+				  acc_cfg->reg_uri.slen, 0);
+	if (reg_uri == NULL) {
+	    pjsua_perror(THIS_FILE, "Invalid registrar URI", 
+			 PJSIP_EINVALIDURI);
+	    return PJSIP_EINVALIDURI;
+	}
+
+	/* Registrar URI MUST be a SIP or SIPS: */
+	if (!PJSIP_URI_SCHEME_IS_SIP(reg_uri) && 
+	    !PJSIP_URI_SCHEME_IS_SIPS(reg_uri)) 
+	{
+	    pjsua_perror(THIS_FILE, "Invalid registar URI", 
+			 PJSIP_EINVALIDSCHEME);
+	    return PJSIP_EINVALIDSCHEME;
+	}
+
+	sip_reg_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(reg_uri);
+
+    } else {
+	sip_reg_uri = NULL;
+    }
+
 
     /* Save the user and domain part. These will be used when finding an 
@@ -136,11 +164,15 @@
      */
     acc->user_part = sip_uri->user;
-    acc->host_part = sip_uri->host;
-
+    acc->srv_domain = sip_uri->host;
+    acc->srv_port = 0;
+
+    if (sip_reg_uri) {
+	acc->srv_port = sip_reg_uri->port;
+    }
 
     /* Create Contact header if not present. */
-    if (acc_cfg->contact.slen == 0) {
-	acc_cfg->contact = acc_cfg->id;
-    }
+    //if (acc_cfg->contact.slen == 0) {
+    //	acc_cfg->contact = acc_cfg->id;
+    //}
 
     PJ_TODO(attach_account_to_transport);
@@ -704,10 +736,22 @@
     sip_uri = pjsip_uri_get_uri(uri);
 
-    /* Find matching domain */
+    /* Find matching domain AND port */
     for (acc_id=0; acc_id<PJ_ARRAY_SIZE(pjsua_var.acc); ++acc_id) {
 	if (!pjsua_var.acc[acc_id].valid)
 	    continue;
-	if (pj_stricmp(&pjsua_var.acc[acc_id].host_part, &sip_uri->host)==0)
+	if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0 &&
+	    pjsua_var.acc[acc_id].srv_port == sip_uri->port)
 	    break;
+    }
+
+    /* If no match, try to match the domain part only */
+    if (acc_id == PJ_ARRAY_SIZE(pjsua_var.acc)) {
+	/* Just use default account */
+	for (acc_id=0; acc_id<PJ_ARRAY_SIZE(pjsua_var.acc); ++acc_id) {
+	    if (!pjsua_var.acc[acc_id].valid)
+		continue;
+	    if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0)
+		break;
+	}
     }
 
@@ -754,5 +798,5 @@
 
 	if (pj_stricmp(&acc->user_part, &sip_uri->user)==0 &&
-	    pj_stricmp(&acc->host_part, &sip_uri->host)==0) 
+	    pj_stricmp(&acc->srv_domain, &sip_uri->host)==0) 
 	{
 	    /* Match ! */
@@ -767,5 +811,5 @@
 	pjsua_acc *acc = &pjsua_var.acc[acc_id];
 
-	if (pj_stricmp(&acc->host_part, &sip_uri->host)==0) {
+	if (pj_stricmp(&acc->srv_domain, &sip_uri->host)==0) {
 	    /* Match ! */
 	    PJSUA_UNLOCK();
Index: /pjproject/trunk/pjsip/src/pjsua-lib/pjsua_call.c
===================================================================
--- /pjproject/trunk/pjsip/src/pjsua-lib/pjsua_call.c	(revision 596)
+++ /pjproject/trunk/pjsip/src/pjsua-lib/pjsua_call.c	(revision 597)
@@ -235,5 +235,5 @@
     /* Create outgoing dialog: */
     status = pjsip_dlg_create_uac( pjsip_ua_instance(), 
-				   &acc->cfg.id, &acc->cfg.contact,
+				   &acc->cfg.id, &acc->real_contact,
 				   dest_uri, dest_uri, &dlg);
     if (status != PJ_SUCCESS) {
@@ -442,5 +442,5 @@
     /* Create dialog: */
     status = pjsip_dlg_create_uas( pjsip_ua_instance(), rdata,
-				   &pjsua_var.acc[acc_id].cfg.contact, 
+				   &pjsua_var.acc[acc_id].real_contact, 
 				   &dlg);
     if (status != PJ_SUCCESS) {
Index: /pjproject/trunk/pjsip/src/pjsua-lib/pjsua_core.c
===================================================================
--- /pjproject/trunk/pjsip/src/pjsua-lib/pjsua_core.c	(revision 596)
+++ /pjproject/trunk/pjsip/src/pjsua-lib/pjsua_core.c	(revision 597)
@@ -69,11 +69,13 @@
 static pj_bool_t logging_on_rx_msg(pjsip_rx_data *rdata)
 {
-    PJ_LOG(4,(THIS_FILE, "RX %d bytes %s from %s:%d:\n"
-			 "%s\n"
+    PJ_LOG(4,(THIS_FILE, "RX %d bytes %s from %s %s:%d:\n"
+			 "%.*s\n"
 			 "--end msg--",
 			 rdata->msg_info.len,
 			 pjsip_rx_data_get_info(rdata),
+			 rdata->tp_info.transport->type_name,
 			 rdata->pkt_info.src_name,
 			 rdata->pkt_info.src_port,
+			 (int)rdata->msg_info.len,
 			 rdata->msg_info.msg_buf));
     
@@ -92,11 +94,13 @@
      */
 
-    PJ_LOG(4,(THIS_FILE, "TX %d bytes %s to %s:%d:\n"
-			 "%s\n"
+    PJ_LOG(4,(THIS_FILE, "TX %d bytes %s to %s %s:%d:\n"
+			 "%.*s\n"
 			 "--end msg--",
 			 (tdata->buf.cur - tdata->buf.start),
 			 pjsip_tx_data_get_info(tdata),
+			 tdata->tp_info.transport->type_name,
 			 tdata->tp_info.dst_name,
 			 tdata->tp_info.dst_port,
+			 (int)(tdata->buf.cur - tdata->buf.start),
 			 tdata->buf.start));
 
Index: /pjproject/trunk/pjsip/src/pjsua-lib/pjsua_im.c
===================================================================
--- /pjproject/trunk/pjsip/src/pjsua-lib/pjsua_im.c	(revision 596)
+++ /pjproject/trunk/pjsip/src/pjsua-lib/pjsua_im.c	(revision 597)
@@ -441,5 +441,5 @@
 	pjsip_generic_string_hdr_create(tdata->pool, 
 					&STR_CONTACT,
-					&pjsua_var.acc[acc_id].cfg.contact));
+					&pjsua_var.acc[acc_id].real_contact));
 
     /* Create IM data to keep message details and give it back to
@@ -522,5 +522,5 @@
 	pjsip_generic_string_hdr_create(tdata->pool, 
 					&STR_CONTACT,
-					&pjsua_var.acc[acc_id].cfg.contact));
+					&pjsua_var.acc[acc_id].real_contact));
 
 
Index: /pjproject/trunk/pjsip/src/pjsua-lib/pjsua_pres.c
===================================================================
--- /pjproject/trunk/pjsip/src/pjsua-lib/pjsua_pres.c	(revision 596)
+++ /pjproject/trunk/pjsip/src/pjsua-lib/pjsua_pres.c	(revision 597)
@@ -466,5 +466,5 @@
     /* Create UAS dialog: */
     status = pjsip_dlg_create_uas(pjsip_ua_instance(), rdata, 
-				  &acc->cfg.contact,
+				  &acc->real_contact,
 				  &dlg);
     if (status != PJ_SUCCESS) {
@@ -771,8 +771,11 @@
     acc = &pjsua_var.acc[acc_id];
 
+    PJ_LOG(4,(THIS_FILE, "Using account %d for buddy %d subscription",
+			 acc_id, index));
+
     /* Create UAC dialog */
     status = pjsip_dlg_create_uac( pjsip_ua_instance(), 
 				   &acc->cfg.id,
-				   &acc->cfg.contact,
+				   &acc->real_contact,
 				   &buddy->uri,
 				   NULL, &dlg);
