Index: pjproject/trunk/pjsip/src/pjsip-ua/sip_reg.c
===================================================================
--- pjproject/trunk/pjsip/src/pjsip-ua/sip_reg.c	(revision 1417)
+++ pjproject/trunk/pjsip/src/pjsip-ua/sip_reg.c	(revision 1431)
@@ -66,6 +66,5 @@
     pjsip_from_hdr		*from_hdr;
     pjsip_to_hdr		*to_hdr;
-    char			*contact_buf;
-    pjsip_generic_string_hdr	*contact_hdr;
+    pjsip_hdr			 contact_hdr_list;
     pjsip_expires_hdr		*expires_hdr;
     pjsip_contact_hdr		*unreg_contact_hdr;
@@ -110,5 +109,4 @@
     regc->token = token;
     regc->cb = cb;
-    regc->contact_buf = (char*)pj_pool_alloc(pool, PJSIP_REGC_CONTACT_BUF_SIZE);
     regc->expires = PJSIP_REGC_EXPIRATION_NOT_SPECIFIED;
 
@@ -119,4 +117,5 @@
     pj_list_init(&regc->route_set);
     pj_list_init(&regc->hdr_list);
+    pj_list_init(&regc->contact_hdr_list);
 
     /* Done */
@@ -191,28 +190,24 @@
 				const pj_str_t contact[] )
 {
+    const pj_str_t CONTACT = { "Contact", 7 };
     int i;
-    char *s;
-    const pj_str_t contact_STR = { "Contact", 7};
-
-    /* Concatenate contacts. */
-    for (i=0, s=regc->contact_buf; i<contact_cnt; ++i) {
-	if ((s-regc->contact_buf) + contact[i].slen + 2 > PJSIP_REGC_CONTACT_BUF_SIZE) {
-	    return PJSIP_EURITOOLONG;
+    
+    /* Clear existing contacts */
+    pj_list_init(&regc->contact_hdr_list);
+
+    for (i=0; i<contact_cnt; ++i) {
+	pjsip_hdr *hdr;
+	pj_str_t tmp;
+
+	pj_strdup_with_null(regc->pool, &tmp, &contact[i]);
+	hdr = pjsip_parse_hdr(regc->pool, &CONTACT, tmp.ptr, tmp.slen, NULL);
+	if (hdr == NULL) {
+	    PJ_LOG(4,(THIS_FILE, "Invalid Contact URI: \"%.*s\"", 
+		     (int)tmp.slen, tmp.ptr));
+	    return PJSIP_EINVALIDURI;
 	}
-	pj_memcpy(s, contact[i].ptr, contact[i].slen);
-	s += contact[i].slen;
-
-	if (i != contact_cnt - 1) {
-	    *s++ = ',';
-	    *s++ = ' ';
-	}
-    }
-
-    /* Set "Contact" header. */
-    regc->contact_hdr = pjsip_generic_string_hdr_create(regc->pool, 
-							&contact_STR,
-							NULL);
-    regc->contact_hdr->hvalue.ptr = regc->contact_buf;
-    regc->contact_hdr->hvalue.slen = (s - regc->contact_buf);
+
+	pj_list_push_back(&regc->contact_hdr_list, hdr);
+    }
 
     return PJ_SUCCESS;
@@ -425,4 +420,5 @@
 {
     pjsip_msg *msg;
+    pjsip_hdr *hdr;
     pj_status_t status;
     pjsip_tx_data *tdata;
@@ -434,9 +430,14 @@
 	return status;
 
-    /* Add Contact header. */
     msg = tdata->msg;
-    pjsip_msg_add_hdr(msg, (pjsip_hdr*)
-			   pjsip_hdr_shallow_clone(tdata->pool, 
-						   regc->contact_hdr));
+
+    /* Add Contact headers. */
+    hdr = regc->contact_hdr_list.next;
+    while (hdr != &regc->contact_hdr_list) {
+	pjsip_msg_add_hdr(msg, (pjsip_hdr*)
+			       pjsip_hdr_shallow_clone(tdata->pool, hdr));
+	hdr = hdr->next;
+    }
+
     if (regc->expires_hdr)
 	pjsip_msg_add_hdr(msg, (pjsip_hdr*)
@@ -462,4 +463,5 @@
     pjsip_tx_data *tdata;
     pjsip_msg *msg;
+    pjsip_hdr *hdr;
     pj_status_t status;
 
@@ -476,7 +478,13 @@
 
     msg = tdata->msg;
-    pjsip_msg_add_hdr(msg, (pjsip_hdr*)
-			   pjsip_hdr_shallow_clone(tdata->pool, 
-						   regc->contact_hdr));
+
+    /* Add Contact headers. */
+    hdr = regc->contact_hdr_list.next;
+    while (hdr != &regc->contact_hdr_list) {
+	pjsip_msg_add_hdr(msg, (pjsip_hdr*)
+			       pjsip_hdr_shallow_clone(tdata->pool, hdr));
+	hdr = hdr->next;
+    }
+
     pjsip_msg_add_hdr( msg, (pjsip_hdr*)regc->unreg_expires_hdr);
 
@@ -681,6 +689,22 @@
 	    for (i=0; i<contact_cnt; ++i) {
 		hdr = contact[i];
-		if (hdr->expires >= 0 && hdr->expires < expiration)
-		    expiration = contact[i]->expires;
+		if (hdr->expires >= 0 && hdr->expires < expiration) {
+		    pjsip_contact_hdr *our_contact;
+
+		    our_contact = (pjsip_contact_hdr*)
+				  regc->contact_hdr_list.next;
+		    if ((void*)our_contact==(void*)&regc->contact_hdr_list.next)
+			continue;
+
+		    /* Only set expiration time if this is the same Contact
+		     * that we register.
+		     */
+		    if (pjsip_uri_cmp(PJSIP_URI_IN_CONTACT_HDR, 
+				      hdr->uri, 
+				      our_contact->uri)==0) 
+		    {
+			expiration = contact[i]->expires;
+		    }
+		}
 	    }
 
