Index: /pjproject/trunk/pjsip/include/pjsua-lib/pjsua_internal.h
===================================================================
--- /pjproject/trunk/pjsip/include/pjsua-lib/pjsua_internal.h	(revision 4541)
+++ /pjproject/trunk/pjsip/include/pjsua-lib/pjsua_internal.h	(revision 4542)
@@ -264,4 +264,6 @@
     pjsip_evsub	    *mwi_sub;	    /**< MWI client subscription	*/
     pjsip_dialog    *mwi_dlg;	    /**< Dialog for MWI sub.		*/
+
+    pj_uint16_t      next_rtp_port; /**< Next RTP port to be used.      */
 } pjsua_acc;
 
Index: /pjproject/trunk/pjsip/src/pjsua-lib/pjsua_acc.c
===================================================================
--- /pjproject/trunk/pjsip/src/pjsua-lib/pjsua_acc.c	(revision 4541)
+++ /pjproject/trunk/pjsip/src/pjsua-lib/pjsua_acc.c	(revision 4542)
@@ -653,4 +653,5 @@
     pj_bzero(&acc->via_addr, sizeof(acc->via_addr));
     acc->via_tp = NULL;
+    acc->next_rtp_port = 0;
 
     /* Remove from array */
Index: /pjproject/trunk/pjsip/src/pjsua-lib/pjsua_media.c
===================================================================
--- /pjproject/trunk/pjsip/src/pjsua-lib/pjsua_media.c	(revision 4541)
+++ /pjproject/trunk/pjsip/src/pjsua-lib/pjsua_media.c	(revision 4542)
@@ -30,7 +30,4 @@
 #endif
 
-/* Next RTP port to be used */
-static pj_uint16_t next_rtp_port;
-
 static void pjsua_media_config_dup(pj_pool_t *pool,
 				   pjsua_media_config *dst,
@@ -220,7 +217,4 @@
     }
 
-    /* Reset RTP port */
-    next_rtp_port = 0;
-
     pj_log_pop_indent();
 
@@ -247,7 +241,7 @@
     char addr_buf[PJ_INET6_ADDRSTRLEN+10];
     pj_sock_t sock[2];
-
-    use_ipv6 = (pjsua_var.acc[call_med->call->acc_id].cfg.ipv6_media_use !=
-		PJSUA_IPV6_DISABLED);
+    pjsua_acc *acc = &pjsua_var.acc[call_med->call->acc_id];
+
+    use_ipv6 = (acc->cfg.ipv6_media_use != PJSUA_IPV6_DISABLED);
     af = use_ipv6 ? pj_AF_INET6() : pj_AF_INET();
 
@@ -261,9 +255,9 @@
     }
 
-    if (next_rtp_port == 0)
-	next_rtp_port = (pj_uint16_t)cfg->port;
-
-    if (next_rtp_port == 0)
-	next_rtp_port = (pj_uint16_t)40000;
+    if (acc->next_rtp_port == 0)
+	acc->next_rtp_port = (pj_uint16_t)cfg->port;
+
+    if (acc->next_rtp_port == 0)
+	acc->next_rtp_port = (pj_uint16_t)DEFAULT_RTP_PORT;
 
     for (i=0; i<2; ++i)
@@ -281,10 +275,11 @@
 
     /* Loop retry to bind RTP and RTCP sockets. */
-    for (i=0; i<RTP_RETRY; ++i, next_rtp_port += 2) {
+    for (i=0; i<RTP_RETRY; ++i, acc->next_rtp_port += 2) {
 
         if (cfg->port > 0 && cfg->port_range > 0 &&
-            next_rtp_port > cfg->port + cfg->port_range)
+            (acc->next_rtp_port > cfg->port + cfg->port_range ||
+             acc->next_rtp_port < cfg->port))
         {
-            next_rtp_port = (pj_uint16_t)cfg->port;
+            acc->next_rtp_port = (pj_uint16_t)cfg->port;
         }
 
@@ -302,5 +297,5 @@
 
 	/* Bind RTP socket */
-	pj_sockaddr_set_port(&bound_addr, next_rtp_port);
+	pj_sockaddr_set_port(&bound_addr, acc->next_rtp_port);
 	status=pj_sock_bind(sock[0], &bound_addr,
 	                    pj_sockaddr_get_len(&bound_addr));
@@ -325,5 +320,5 @@
 
 	/* Bind RTCP socket */
-	pj_sockaddr_set_port(&bound_addr, (pj_uint16_t)(next_rtp_port+1));
+	pj_sockaddr_set_port(&bound_addr, (pj_uint16_t)(acc->next_rtp_port+1));
 	status=pj_sock_bind(sock[1], &bound_addr,
 	                    pj_sockaddr_get_len(&bound_addr));
@@ -417,10 +412,10 @@
 
 	    status = pj_sockaddr_init(af, &mapped_addr[0], &cfg->public_addr,
-				      (pj_uint16_t)next_rtp_port);
+				      (pj_uint16_t)acc->next_rtp_port);
 	    if (status != PJ_SUCCESS)
 		goto on_error;
 
 	    status = pj_sockaddr_init(af, &mapped_addr[1], &cfg->public_addr,
-				      (pj_uint16_t)(next_rtp_port+1));
+				      (pj_uint16_t)(acc->next_rtp_port+1));
 	    if (status != PJ_SUCCESS)
 		goto on_error;
@@ -445,5 +440,5 @@
 		pj_sockaddr_copy_addr(&mapped_addr[i], &bound_addr);
 		pj_sockaddr_set_port(&mapped_addr[i],
-		                     (pj_uint16_t)(next_rtp_port+i));
+		                     (pj_uint16_t)(acc->next_rtp_port+i));
 	    }
 
@@ -472,5 +467,5 @@
 				sizeof(addr_buf), 3)));
 
-    next_rtp_port += 2;
+    acc->next_rtp_port += 2;
     return PJ_SUCCESS;
 
