Index: /pjproject/trunk/pjsip/src/pjsip/sip_transport.c
===================================================================
--- /pjproject/trunk/pjsip/src/pjsip/sip_transport.c	(revision 6089)
+++ /pjproject/trunk/pjsip/src/pjsip/sip_transport.c	(revision 6090)
@@ -50,4 +50,22 @@
 		 str, sizeof(str));
     return str;
+}
+static const char* print_tpsel_info(const pjsip_tpselector *sel)
+{
+    static char tpsel_info_buf[80];
+    if (!sel) return "(null)";
+    if (sel->type==PJSIP_TPSELECTOR_LISTENER)
+	pj_ansi_snprintf(tpsel_info_buf, sizeof(tpsel_info_buf),
+			 "listener[%s], reuse=%d", sel->u.listener->obj_name,
+			 !sel->disable_connection_reuse);
+    else if (sel->type==PJSIP_TPSELECTOR_TRANSPORT)
+	pj_ansi_snprintf(tpsel_info_buf, sizeof(tpsel_info_buf),
+			 "transport[%s], reuse=%d", sel->u.transport->info,
+			 !sel->disable_connection_reuse);
+    else
+	pj_ansi_snprintf(tpsel_info_buf, sizeof(tpsel_info_buf),
+			 "unknown[%p], reuse=%d", sel->u.ptr,
+			 !sel->disable_connection_reuse);
+    return tpsel_info_buf;
 }
 #else
@@ -1226,8 +1244,12 @@
 	 */
 	pj_list_push_back(tp_ref, tp_add);
+	TRACE_((THIS_FILE, "Remote address already registered, "
+			   "appended the transport to the list"));
     } else {
 	/* Transport list not found, add it to the hash table. */
 	pj_hash_set_np(mgr->table, &tp->key, key_len, hval, tp_add->tp_buf,
 		       tp_add);
+	TRACE_((THIS_FILE, "Remote address not registered, "
+			   "added the transport to the hash"));
     }
 
@@ -1299,4 +1321,11 @@
 			pj_hash_set_np(mgr->table, &tp_next->tp->key, key_len,
 				       hval, tp_next->tp_buf, tp_next);
+			TRACE_((THIS_FILE, "Hash entry updated after "
+					   "transport %d being destroyed",
+					   tp->obj_name));
+		    } else {
+			TRACE_((THIS_FILE, "Hash entry deleted after "
+					   "transport %d being destroyed",
+					   tp->obj_name));
 		    }
 		}
@@ -1310,4 +1339,12 @@
 	    tp_iter = tp_iter->next;
 	} while (tp_iter != tp_ref);
+
+	if (tp_iter->tp != tp) {
+	    PJ_LOG(3, (THIS_FILE, "Warning: transport %s being destroyed is "
+				  "not registered", tp->obj_name));
+	}
+    } else {
+	PJ_LOG(3, (THIS_FILE, "Warning: transport %s being destroyed is "
+			      "not found in the hash table", tp->obj_name));
     }
 
@@ -2175,4 +2212,5 @@
 }
 
+
 /*
  * pjsip_tpmgr_acquire_transport2()
@@ -2192,6 +2230,7 @@
     pj_status_t status;
 
-    TRACE_((THIS_FILE,"Acquiring transport type=%s, remote=%s:%d",
+    TRACE_((THIS_FILE,"Acquiring transport type=%s, sel=%s remote=%s:%d",
 		       pjsip_transport_get_type_name(type),
+		       print_tpsel_info(sel),
 		       addr_string(remote),
 		       pj_sockaddr_get_port(remote)));
@@ -2210,4 +2249,5 @@
 	if (seltp->key.type != type) {
 	    pj_lock_release(mgr->lock);
+	    TRACE_((THIS_FILE, "Transport type in tpsel not matched"));
 	    return PJSIP_ETPNOTSUITABLE;
 	}
@@ -2250,4 +2290,5 @@
 	    if (sel->u.listener->type != type) {
 		pj_lock_release(mgr->lock);
+		TRACE_((THIS_FILE, "Listener type in tpsel not matched"));
 		return PJSIP_ETPNOTSUITABLE;
 	    }
@@ -2265,19 +2306,23 @@
 						NULL);
 	    if (tp_entry) {
-		if (sel && sel->type == PJSIP_TPSELECTOR_LISTENER) {
-		    transport *tp_iter = tp_entry;
-		    do {
+		transport *tp_iter = tp_entry;
+		do {
+		    /* Don't use transport being shutdown */
+		    if (!tp_iter->tp->is_shutdown) {
 			if (sel && sel->type == PJSIP_TPSELECTOR_LISTENER &&
-			    sel->u.listener &&
-			    tp_iter->tp->factory == sel->u.listener)
+			    sel->u.listener)
 			{
+			    /* Match listener if selector is set */
+			    if (tp_iter->tp->factory == sel->u.listener) {
+				tp_ref = tp_iter->tp;
+				break;
+			    }
+			} else {
 			    tp_ref = tp_iter->tp;
 			    break;
 			}
-			tp_iter = tp_iter->next;
-		    } while (tp_iter != tp_entry);
-		} else {
-		    tp_ref = tp_entry->tp;
-		}
+		    }
+		    tp_iter = tp_iter->next;
+		} while (tp_iter != tp_entry);
 	    }
 	}
@@ -2292,5 +2337,5 @@
 	    /* Ignore address for loop transports. */
 	    if (type == PJSIP_TRANSPORT_LOOP ||
-		     type == PJSIP_TRANSPORT_LOOP_DGRAM)
+		type == PJSIP_TRANSPORT_LOOP_DGRAM)
 	    {
 		pj_sockaddr *addr = &key.rem_addr;
@@ -2331,4 +2376,5 @@
 	     * but different factory).
 	     */
+	    TRACE_((THIS_FILE, "Transport found but from different listener"));
 	}
 
@@ -2363,8 +2409,11 @@
 
 	    /* Verify that the listener type matches the destination type */
+	    /* Already checked above. */
+	    /*
 	    if (sel->u.listener->type != type) {
 		pj_lock_release(mgr->lock);
 		return PJSIP_ETPNOTSUITABLE;
 	    }
+	    */
 
 	    /* We'll use this listener to create transport */
