Index: /pjproject/trunk/pjsip-apps/src/pjsua/pjsua_app.c
===================================================================
--- /pjproject/trunk/pjsip-apps/src/pjsua/pjsua_app.c	(revision 736)
+++ /pjproject/trunk/pjsip-apps/src/pjsua/pjsua_app.c	(revision 737)
@@ -2139,4 +2139,7 @@
 		    pjsua_call_xfer( current_call, &tmp, &msg_data);
 		}
+
+		/* Hangup call regardless of xfer status */
+		pjsua_call_hangup(current_call, PJSIP_SC_GONE, NULL, NULL);
 	    }
 	    break;
Index: /pjproject/trunk/pjsip/include/pjsua-lib/pjsua.h
===================================================================
--- /pjproject/trunk/pjsip/include/pjsua-lib/pjsua.h	(revision 736)
+++ /pjproject/trunk/pjsip/include/pjsua-lib/pjsua.h	(revision 737)
@@ -1021,8 +1021,23 @@
 
 /**
+ * Default account priority.
+ */
+#ifndef PJSUA_DEFAULT_ACC_PRIORITY
+#   define PJSUA_DEFAULT_ACC_PRIORITY	0
+#endif
+
+
+/**
  * Account configuration.
  */
 typedef struct pjsua_acc_config
 {
+    /**
+     * Account priority, which is used to control the order of matching
+     * incoming/outgoing requests. The higher the number means the higher
+     * the priority is, and the account will be matched first.
+     */
+    int		    priority;
+
     /** 
      * The full SIP URL for the account. The value can take name address or 
Index: /pjproject/trunk/pjsip/include/pjsua-lib/pjsua_internal.h
===================================================================
--- /pjproject/trunk/pjsip/include/pjsua-lib/pjsua_internal.h	(revision 736)
+++ /pjproject/trunk/pjsip/include/pjsua-lib/pjsua_internal.h	(revision 737)
@@ -190,4 +190,5 @@
     pjsua_acc_id	 default_acc;	     /**< Default account ID	*/
     pjsua_acc		 acc[PJSUA_MAX_ACC]; /**< Account array.	*/
+    pjsua_acc_id	 acc_ids[PJSUA_MAX_ACC]; /**< Acc sorted by prio*/
 
     /* Calls: */
Index: /pjproject/trunk/pjsip/src/pjsua-lib/pjsua_acc.c
===================================================================
--- /pjproject/trunk/pjsip/src/pjsua-lib/pjsua_acc.c	(revision 736)
+++ /pjproject/trunk/pjsip/src/pjsua-lib/pjsua_acc.c	(revision 737)
@@ -230,4 +230,14 @@
     pjsua_var.acc[acc_id].valid = PJ_TRUE;
 
+    /* Insert account ID into account ID array, sorted by priority */
+    for (i=0; i<pjsua_var.acc_cnt; ++i) {
+	if ( pjsua_var.acc[pjsua_var.acc_ids[i]].cfg.priority <
+	     pjsua_var.acc[acc_id].cfg.priority)
+	{
+	    break;
+	}
+    }
+    pj_array_insert(pjsua_var.acc_ids, sizeof(pjsua_var.acc_ids[0]),
+		    pjsua_var.acc_cnt, i, &acc_id);
 
     return PJ_SUCCESS;
@@ -321,4 +331,7 @@
     
     pjsua_acc_config_default(&cfg);
+
+    /* Lower the priority of local account */
+    --cfg.priority;
 
     /* Build URI for the account */
@@ -341,4 +354,6 @@
 PJ_DEF(pj_status_t) pjsua_acc_del(pjsua_acc_id acc_id)
 {
+    unsigned i;
+
     PJ_ASSERT_RETURN(acc_id>=0 && acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc),
 		     PJ_EINVAL);
@@ -356,4 +371,15 @@
     /* Invalidate */
     pjsua_var.acc[acc_id].valid = PJ_FALSE;
+
+    /* Remove from array */
+    for (i=0; i<pjsua_var.acc_cnt; ++i) {
+	if (pjsua_var.acc_ids[i] == acc_id)
+	    break;
+    }
+    if (i != pjsua_var.acc_cnt) {
+	pj_array_erase(pjsua_var.acc_ids, sizeof(pjsua_var.acc_ids[0]),
+		       pjsua_var.acc_cnt, i);
+	--pjsua_var.acc_cnt;
+    }
 
     PJ_TODO(may_need_to_scan_calls);
@@ -710,5 +736,5 @@
     pjsip_uri *uri;
     pjsip_sip_uri *sip_uri;
-    unsigned acc_id;
+    unsigned i;
 
     PJSUA_LOCK();
@@ -720,6 +746,6 @@
     uri = pjsip_parse_uri(pjsua_var.pool, tmp.ptr, tmp.slen, 0);
     if (!uri) {
-	acc_id = pjsua_var.default_acc;
-	goto on_return;
+	PJSUA_UNLOCK();
+	return pjsua_var.default_acc;
     }
 
@@ -728,64 +754,49 @@
     {
 	/* Return the first account with proxy */
-	for (acc_id=0; acc_id<PJ_ARRAY_SIZE(pjsua_var.acc); ++acc_id) {
-	    if (!pjsua_var.acc[acc_id].valid)
+	for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
+	    if (!pjsua_var.acc[i].valid)
 		continue;
-	    if (!pj_list_empty(&pjsua_var.acc[acc_id].route_set))
+	    if (!pj_list_empty(&pjsua_var.acc[i].route_set))
 		break;
 	}
 
-	if (acc_id != PJ_ARRAY_SIZE(pjsua_var.acc)) {
+	if (i != PJ_ARRAY_SIZE(pjsua_var.acc)) {
 	    /* Found rather matching account */
-	    goto on_return;
+	    PJSUA_UNLOCK();
+	    return 0;
 	}
 
 	/* Not found, use default account */
-	acc_id = pjsua_var.default_acc;
-	goto on_return;
+	PJSUA_UNLOCK();
+	return pjsua_var.default_acc;
     }
 
     sip_uri = pjsip_uri_get_uri(uri);
 
-    /* See if default acc match */
-    if (pjsua_var.default_acc != PJSUA_INVALID_ID &&
-	pj_stricmp(&pjsua_var.acc[pjsua_var.default_acc].srv_domain, &sip_uri->host)==0 &&
-	pjsua_var.acc[pjsua_var.default_acc].srv_port == sip_uri->port) 
-    {
-	acc_id = pjsua_var.default_acc;
-    } else {
-	acc_id = PJ_ARRAY_SIZE(pjsua_var.acc);
-    }
-
     /* Find matching domain AND port */
-    if (acc_id == PJ_ARRAY_SIZE(pjsua_var.acc)) {
-	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 &&
-		pjsua_var.acc[acc_id].srv_port == sip_uri->port)
-		break;
+    for (i=0; i<pjsua_var.acc_cnt; ++i) {
+	unsigned acc_id = pjsua_var.acc_ids[i];
+	if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0 &&
+	    pjsua_var.acc[acc_id].srv_port == sip_uri->port)
+	{
+	    PJSUA_UNLOCK();
+	    return acc_id;
 	}
     }
 
     /* 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;
-	}
-    }
-
-    if (acc_id == PJ_ARRAY_SIZE(pjsua_var.acc)) {
-	/* Just use default account */
-	acc_id = pjsua_var.default_acc;
-    }
-
-on_return:
+    for (i=0; i<pjsua_var.acc_cnt; ++i) {
+	unsigned acc_id = pjsua_var.acc_ids[i];
+	if (pj_stricmp(&pjsua_var.acc[acc_id].srv_domain, &sip_uri->host)==0)
+	{
+	    PJSUA_UNLOCK();
+	    return acc_id;
+	}
+    }
+
+
+    /* Still no match, just use default account */
     PJSUA_UNLOCK();
-
-    return acc_id;
+    return pjsua_var.default_acc;
 }
 
@@ -799,5 +810,5 @@
     pjsip_uri *uri;
     pjsip_sip_uri *sip_uri;
-    unsigned acc_id;
+    unsigned i;
 
     uri = rdata->msg_info.to->uri;
@@ -816,6 +827,6 @@
 
     /* Find account which has matching username and domain. */
-    for (acc_id=0; acc_id < pjsua_var.acc_cnt; ++acc_id) {
-
+    for (i=0; i < pjsua_var.acc_cnt; ++i) {
+	unsigned acc_id = pjsua_var.acc_ids[i];
 	pjsua_acc *acc = &pjsua_var.acc[acc_id];
 
@@ -829,10 +840,22 @@
     }
 
-    /* No matching, try match domain part only. */
-    for (acc_id=0; acc_id < pjsua_var.acc_cnt; ++acc_id) {
-
+    /* No matching account, try match domain part only. */
+    for (i=0; i < pjsua_var.acc_cnt; ++i) {
+	unsigned acc_id = pjsua_var.acc_ids[i];
 	pjsua_acc *acc = &pjsua_var.acc[acc_id];
 
 	if (pj_stricmp(&acc->srv_domain, &sip_uri->host)==0) {
+	    /* Match ! */
+	    PJSUA_UNLOCK();
+	    return acc_id;
+	}
+    }
+
+    /* No matching account, try match user part only. */
+    for (i=0; i < pjsua_var.acc_cnt; ++i) {
+	unsigned acc_id = pjsua_var.acc_ids[i];
+	pjsua_acc *acc = &pjsua_var.acc[acc_id];
+
+	if (pj_stricmp(&acc->user_part, &sip_uri->user)==0) {
 	    /* Match ! */
 	    PJSUA_UNLOCK();
Index: /pjproject/trunk/pjsip/src/pjsua-lib/pjsua_call.c
===================================================================
--- /pjproject/trunk/pjsip/src/pjsua-lib/pjsua_call.c	(revision 736)
+++ /pjproject/trunk/pjsip/src/pjsua-lib/pjsua_call.c	(revision 737)
@@ -238,4 +238,7 @@
     call = &pjsua_var.calls[call_id];
 
+    PJ_LOG(4,(THIS_FILE, "Making call with acc #%d to %.*s", acc_id,
+	      (int)dest_uri->slen, dest_uri->ptr));
+
     /* Mark call start time. */
     pj_gettimeofday(&call->start_time);
@@ -316,4 +319,7 @@
 
     pjsua_process_msg_data( tdata, msg_data);
+
+    /* Must increment call counter now */
+    ++pjsua_var.call_cnt;
 
     /* Send initial INVITE: */
@@ -333,6 +339,4 @@
 
     /* Done. */
-
-    ++pjsua_var.call_cnt;
 
     if (p_call_id)
@@ -463,5 +467,5 @@
      * the call.
      */
-    acc_id = pjsua_acc_find_for_incoming(rdata);
+    acc_id = call->acc_id = pjsua_acc_find_for_incoming(rdata);
 
     /* Get suitable Contact header */
