Index: pjproject/trunk/pjsip-apps/src/test-pjsua/scripts-sendto/360_non_sip_uri.py
===================================================================
--- pjproject/trunk/pjsip-apps/src/test-pjsua/scripts-sendto/360_non_sip_uri.py	(revision 2194)
+++ pjproject/trunk/pjsip-apps/src/test-pjsua/scripts-sendto/360_non_sip_uri.py	(revision 2194)
@@ -0,0 +1,27 @@
+# $Id:$
+import inc_sip as sip
+import inc_sdp as sdp
+
+# Some non-SIP URI's in Contact header
+#
+complete_msg = \
+"""INVITE sip:localhost SIP/2.0
+Via: SIP/2.0/UDP 192.168.0.14:5060;rport;branch=z9hG4bKPj9db9
+Max-Forwards: 70
+From: <sip:192.168.0.14>;tag=08cd5bfc2d8a4fddb1f5e59c6961d298
+To: <sip:localhost>
+Call-ID: 3373d9eb32aa458db7e69c7ea51e0bd7
+CSeq: 0 INVITE
+Contact: mailto:dontspam@pjsip.org
+Contact: <mailto:dontspam@pjsip.org>
+Contact: http://www.pjsip.org/the%20path.cgi?pname=pvalue
+Contact: <sip:localhost>
+User-Agent: PJSUA v0.9.0-trunk/win32
+Content-Length: 0
+"""
+
+
+sendto_cfg = sip.SendtoCfg( "Non SIP URI in Contact", 
+			    "--null-audio --auto-answer 200", 
+			    "", 200, complete_msg=complete_msg)
+
Index: pjproject/trunk/pjsip-apps/src/test-pjsua/scripts-sendto/361_non_sip_uri.py
===================================================================
--- pjproject/trunk/pjsip-apps/src/test-pjsua/scripts-sendto/361_non_sip_uri.py	(revision 2194)
+++ pjproject/trunk/pjsip-apps/src/test-pjsua/scripts-sendto/361_non_sip_uri.py	(revision 2194)
@@ -0,0 +1,26 @@
+# $Id:$
+import inc_sip as sip
+import inc_sdp as sdp
+
+# No SIP URI in Contact header 
+#
+complete_msg = \
+"""INVITE sip:localhost SIP/2.0
+Via: SIP/2.0/UDP 192.168.0.14:5060;rport;branch=z9hG4bKPj9db9
+Max-Forwards: 70
+From: <sip:192.168.0.14>;tag=08cd5bfc2d8a4fddb1f5e59c6961d298
+To: <sip:localhost>
+Call-ID: 3373d9eb32aa458db7e69c7ea51e0bd7
+CSeq: 0 INVITE
+Contact: mailto:dontspam@pjsip.org
+Contact: <mailto:dontspam@pjsip.org>
+Contact: http://www.pjsip.org/the%20path.cgi?pname=pvalue
+User-Agent: PJSUA v0.9.0-trunk/win32
+Content-Length: 0
+"""
+
+
+sendto_cfg = sip.SendtoCfg( "No SIP URI in Contact", 
+			    "--null-audio --auto-answer 200", 
+			    "", 500, complete_msg=complete_msg)
+
Index: pjproject/trunk/pjsip-apps/src/test-pjsua/scripts-sendto/362_non_sip_uri.py
===================================================================
--- pjproject/trunk/pjsip-apps/src/test-pjsua/scripts-sendto/362_non_sip_uri.py	(revision 2194)
+++ pjproject/trunk/pjsip-apps/src/test-pjsua/scripts-sendto/362_non_sip_uri.py	(revision 2194)
@@ -0,0 +1,27 @@
+# $Id:$
+import inc_sip as sip
+import inc_sdp as sdp
+
+# Some non-SIP URI's in Contact header
+#
+complete_msg = \
+"""INVITE sip:localhost SIP/2.0
+Via: SIP/2.0/UDP 192.168.0.14:5060;rport;branch=z9hG4bKPj9db9
+Max-Forwards: 70
+From: <sip:192.168.0.14>;tag=08cd5bfc2d8a4fddb1f5e59c6961d298
+To: <sip:localhost>
+Call-ID: 3373d9eb32aa458db7e69c7ea51e0bd7
+CSeq: 0 INVITE
+Contact: <sip:localhost>
+Contact: mailto:dontspam@pjsip.org
+Contact: <mailto:dontspam@pjsip.org>
+Contact: http://www.pjsip.org/the%20path.cgi?pname=pvalue
+User-Agent: PJSUA v0.9.0-trunk/win32
+Content-Length: 0
+"""
+
+
+sendto_cfg = sip.SendtoCfg( "Non SIP URI in Contact", 
+			    "--null-audio --auto-answer 200", 
+			    "", 200, complete_msg=complete_msg)
+
Index: pjproject/trunk/pjsip-apps/src/test-pjsua/scripts-sendto/363_non_sip_uri_subscribe.py
===================================================================
--- pjproject/trunk/pjsip-apps/src/test-pjsua/scripts-sendto/363_non_sip_uri_subscribe.py	(revision 2194)
+++ pjproject/trunk/pjsip-apps/src/test-pjsua/scripts-sendto/363_non_sip_uri_subscribe.py	(revision 2194)
@@ -0,0 +1,31 @@
+# $Id:$
+import inc_sip as sip
+import inc_sdp as sdp
+
+# Some non-SIP URI's in Contact header
+#
+complete_msg = \
+"""SUBSCRIBE sip:localhost SIP/2.0
+Via: SIP/2.0/UDP 192.168.0.14:5060;rport;branch=z9hG4bKPj9db9
+Max-Forwards: 70
+From: <sip:192.168.0.14>;tag=08cd5bfc2d8a4fddb1f5e59c6961d298
+To: <sip:localhost>
+Call-ID: 3373d9eb32aa458db7e69c7ea51e0bd7
+CSeq: 0 SUBSCRIBE
+Contact: mailto:dontspam@pjsip.org
+Contact: <mailto:dontspam@pjsip.org>
+Contact: http://www.pjsip.org/the%20path.cgi?pname=pvalue
+Contact: <sip:localhost>
+Event: presence
+Expires: 600
+Accept: application/pidf+xml, application/xpidf+xml
+Allow-Events: presence, refer
+User-Agent: PJSUA v0.9.0-trunk/win32
+Content-Length: 0
+"""
+
+
+sendto_cfg = sip.SendtoCfg( "Non SIP URI in Contact", 
+			    "--null-audio --auto-answer 200", 
+			    "", 200, complete_msg=complete_msg)
+
Index: pjproject/trunk/pjsip-apps/src/test-pjsua/scripts-sendto/364_non_sip_uri_subscribe.py
===================================================================
--- pjproject/trunk/pjsip-apps/src/test-pjsua/scripts-sendto/364_non_sip_uri_subscribe.py	(revision 2194)
+++ pjproject/trunk/pjsip-apps/src/test-pjsua/scripts-sendto/364_non_sip_uri_subscribe.py	(revision 2194)
@@ -0,0 +1,30 @@
+# $Id:$
+import inc_sip as sip
+import inc_sdp as sdp
+
+# Some non-SIP URI's in Contact header
+#
+complete_msg = \
+"""SUBSCRIBE sip:localhost SIP/2.0
+Via: SIP/2.0/UDP 192.168.0.14:5060;rport;branch=z9hG4bKPj9db9
+Max-Forwards: 70
+From: <sip:192.168.0.14>;tag=08cd5bfc2d8a4fddb1f5e59c6961d298
+To: <sip:localhost>
+Call-ID: 3373d9eb32aa458db7e69c7ea51e0bd7
+CSeq: 0 SUBSCRIBE
+Contact: mailto:dontspam@pjsip.org
+Contact: <mailto:dontspam@pjsip.org>
+Contact: http://www.pjsip.org/the%20path.cgi?pname=pvalue
+Event: presence
+Expires: 600
+Accept: application/pidf+xml, application/xpidf+xml
+Allow-Events: presence, refer
+User-Agent: PJSUA v0.9.0-trunk/win32
+Content-Length: 0
+"""
+
+
+sendto_cfg = sip.SendtoCfg( "Non SIP URI in Contact", 
+			    "--null-audio --auto-answer 200", 
+			    "", 400, complete_msg=complete_msg)
+
Index: pjproject/trunk/pjsip/src/pjsip/sip_dialog.c
===================================================================
--- pjproject/trunk/pjsip/src/pjsip/sip_dialog.c	(revision 2193)
+++ pjproject/trunk/pjsip/src/pjsip/sip_dialog.c	(revision 2194)
@@ -308,5 +308,6 @@
 {
     pj_status_t status;
-    pjsip_hdr *contact_hdr;
+    pjsip_hdr *pos = NULL;
+    pjsip_contact_hdr *contact_hdr;
     pjsip_rr_hdr *rr;
     pjsip_transaction *tsx = NULL;
@@ -417,14 +418,31 @@
 
 
-    /* Init remote's contact from Contact header. */
-    contact_hdr = (pjsip_hdr*)
-    		  pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT, 
-				     NULL);
+    /* Init remote's contact from Contact header. 
+     * Iterate the Contact URI until we find sip: or sips: scheme.
+     */
+    do {
+	contact_hdr = (pjsip_contact_hdr*)
+		      pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT,
+				         pos);
+	if (contact_hdr) {
+	    if (!PJSIP_URI_SCHEME_IS_SIP(contact_hdr->uri) && 
+		!PJSIP_URI_SCHEME_IS_SIPS(contact_hdr->uri))
+	    {
+		pos = (pjsip_hdr*)contact_hdr->next;
+		if (pos == &rdata->msg_info.msg->hdr)
+		    contact_hdr = NULL;
+	    } else {
+		break;
+	    }
+	}
+    } while (contact_hdr);
+
     if (!contact_hdr) {
 	status = PJSIP_ERRNO_FROM_SIP_STATUS(PJSIP_SC_BAD_REQUEST);
 	goto on_error;
     }
+
     dlg->remote.contact = (pjsip_contact_hdr*) 
-    			  pjsip_hdr_clone(dlg->pool, contact_hdr);
+    			  pjsip_hdr_clone(dlg->pool, (pjsip_hdr*)contact_hdr);
 
     /* Init remote's CSeq from CSeq header */
Index: pjproject/trunk/pjsip/src/pjsua-lib/pjsua_acc.c
===================================================================
--- pjproject/trunk/pjsip/src/pjsua-lib/pjsua_acc.c	(revision 2193)
+++ pjproject/trunk/pjsip/src/pjsua-lib/pjsua_acc.c	(revision 2194)
@@ -1675,13 +1675,28 @@
 		pjsip_uri_get_uri(rdata->msg_info.record_route->name_addr.uri);
     } else {
+	pjsip_hdr *pos = NULL;
 	pjsip_contact_hdr *h_contact;
 	pjsip_uri *uri = NULL;
 
-	/* Otherwise URI is Contact URI */
-	h_contact = (pjsip_contact_hdr*)
-		    pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT,
-				       NULL);
-	if (h_contact)
-	    uri = (pjsip_uri*) pjsip_uri_get_uri(h_contact->uri);
+	/* Otherwise URI is Contact URI.
+	 * Iterate the Contact URI until we find sip: or sips: scheme.
+	 */
+	do {
+	    h_contact = (pjsip_contact_hdr*)
+			pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT,
+					   pos);
+	    if (h_contact) {
+		uri = (pjsip_uri*) pjsip_uri_get_uri(h_contact->uri);
+		if (!PJSIP_URI_SCHEME_IS_SIP(uri) && 
+		    !PJSIP_URI_SCHEME_IS_SIPS(uri))
+		{
+		    pos = (pjsip_hdr*)h_contact->next;
+		    if (pos == &rdata->msg_info.msg->hdr)
+			h_contact = NULL;
+		} else {
+		    break;
+		}
+	    }
+	} while (h_contact);
 	
 
Index: pjproject/trunk/pjsip/src/pjsua-lib/pjsua_pres.c
===================================================================
--- pjproject/trunk/pjsip/src/pjsua-lib/pjsua_pres.c	(revision 2193)
+++ pjproject/trunk/pjsip/src/pjsua-lib/pjsua_pres.c	(revision 2194)
@@ -689,4 +689,6 @@
 			 status);
 	    PJSUA_UNLOCK();
+	    pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, 400, NULL,
+					  NULL, NULL);
 	    return PJ_TRUE;
 	}
@@ -701,4 +703,6 @@
 		     status);
 	PJSUA_UNLOCK();
+	pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, 400, NULL,
+				      NULL, NULL);
 	return PJ_TRUE;
     }
