Index: /pjproject/branches/1.x/pjsip-apps/src/pjsua/pjsua_app.c
===================================================================
--- /pjproject/branches/1.x/pjsip-apps/src/pjsua/pjsua_app.c	(revision 3828)
+++ /pjproject/branches/1.x/pjsip-apps/src/pjsua/pjsua_app.c	(revision 3829)
@@ -5119,5 +5119,5 @@
     }
 
-    status = pjsua_destroy();
+    status = pjsua_destroy2(1);
 
     pj_bzero(&app_config, sizeof(app_config));
Index: /pjproject/branches/1.x/pjsip/include/pjsua-lib/pjsua.h
===================================================================
--- /pjproject/branches/1.x/pjsip/include/pjsua-lib/pjsua.h	(revision 3828)
+++ /pjproject/branches/1.x/pjsip/include/pjsua-lib/pjsua.h	(revision 3829)
@@ -1295,4 +1295,16 @@
 
 /**
+ * Flags to be given to pjsua_destroy2()
+ */
+typedef enum pjsua_destroy_flag
+{
+    /**
+     * Do not invoke any networking functions.
+     */
+    PJSUA_DESTROY_NO_NETWORK = 1
+
+} pjsua_destroy_flag;
+
+/**
  * Use this function to initialize pjsua config.
  *
@@ -1430,7 +1442,19 @@
  * keep track of it's state.
  *
+ * @see pjsua_destroy2()
+ *
  * @return		PJ_SUCCESS on success, or the appropriate error code.
  */
 PJ_DECL(pj_status_t) pjsua_destroy(void);
+
+
+/**
+ * Variant of destroy with additional flags.
+ *
+ * @param flags		Combination of pjsua_destroy_flag enumeration.
+ *
+ * @return		PJ_SUCCESS on success, or the appropriate error code.
+ */
+PJ_DECL(pj_status_t) pjsua_destroy2(unsigned flags);
 
 
Index: /pjproject/branches/1.x/pjsip/include/pjsua-lib/pjsua_internal.h
===================================================================
--- /pjproject/branches/1.x/pjsip/include/pjsua-lib/pjsua_internal.h	(revision 3828)
+++ /pjproject/branches/1.x/pjsip/include/pjsua-lib/pjsua_internal.h	(revision 3829)
@@ -464,5 +464,5 @@
  * Shutdown presence.
  */
-void pjsua_pres_shutdown(void);
+void pjsua_pres_shutdown(unsigned flags);
 
 /**
@@ -479,10 +479,10 @@
  *  Send un-PUBLISH
  */
-void pjsua_pres_unpublish(pjsua_acc *acc);
+void pjsua_pres_unpublish(pjsua_acc *acc, unsigned flags);
 
 /**
  * Terminate server subscription for the account 
  */
-void pjsua_pres_delete_acc(int acc_id);
+void pjsua_pres_delete_acc(int acc_id, unsigned flags);
 
 /**
@@ -519,5 +519,5 @@
  * Destroy pjsua media subsystem.
  */
-pj_status_t pjsua_media_subsys_destroy(void);
+pj_status_t pjsua_media_subsys_destroy(unsigned flags);
 
 /**
Index: /pjproject/branches/1.x/pjsip/src/pjsua-lib/pjsua_acc.c
===================================================================
--- /pjproject/branches/1.x/pjsip/src/pjsua-lib/pjsua_acc.c	(revision 3828)
+++ /pjproject/branches/1.x/pjsip/src/pjsua-lib/pjsua_acc.c	(revision 3829)
@@ -583,5 +583,5 @@
 
     /* Delete server presence subscription */
-    pjsua_pres_delete_acc(acc_id);
+    pjsua_pres_delete_acc(acc_id, 0);
 
     /* Release account pool */
@@ -802,5 +802,5 @@
 	acc->cfg.publish_enabled = cfg->publish_enabled;
 	if (!acc->cfg.publish_enabled)
-	    pjsua_pres_unpublish(acc);
+	    pjsua_pres_unpublish(acc, 0);
 	else
 	    update_reg = PJ_TRUE;
@@ -1996,5 +1996,5 @@
 	}
 
-	pjsua_pres_unpublish(&pjsua_var.acc[acc_id]);
+	pjsua_pres_unpublish(&pjsua_var.acc[acc_id], 0);
 
 	status = pjsip_regc_unregister(pjsua_var.acc[acc_id].regc, &tdata);
Index: /pjproject/branches/1.x/pjsip/src/pjsua-lib/pjsua_core.c
===================================================================
--- /pjproject/branches/1.x/pjsip/src/pjsua-lib/pjsua_core.c	(revision 3828)
+++ /pjproject/branches/1.x/pjsip/src/pjsua-lib/pjsua_core.c	(revision 3829)
@@ -1284,5 +1284,5 @@
  * Destroy pjsua.
  */
-PJ_DEF(pj_status_t) pjsua_destroy(void)
+PJ_DEF(pj_status_t) pjsua_destroy2(unsigned flags)
 {
     int i;  /* Must be signed */
@@ -1303,8 +1303,10 @@
 	unsigned max_wait;
 
-	PJ_LOG(4,(THIS_FILE, "Shutting down..."));
+	PJ_LOG(4,(THIS_FILE, "Shutting down, flags=%d...", flags));
 
 	/* Terminate all calls. */
-	pjsua_call_hangup_all();
+	if ((flags & PJSUA_DESTROY_NO_NETWORK) == 0) {
+	    pjsua_call_hangup_all();
+	}
 
 	/* Set all accounts to offline */
@@ -1317,8 +1319,8 @@
 
 	/* Terminate all presence subscriptions. */
-	pjsua_pres_shutdown();
+	pjsua_pres_shutdown(flags);
 
 	/* Destroy media (to shutdown media transports etc) */
-	pjsua_media_subsys_destroy();
+	pjsua_media_subsys_destroy(flags);
 
 	/* Wait for sometime until all publish client sessions are done
@@ -1334,4 +1336,9 @@
 	}
 	
+	/* No need to wait if we didn't send anything */
+	if (flags & PJSUA_DESTROY_NO_NETWORK) {
+	    max_wait = 0;
+	}
+
 	/* Second stage, wait for unpublications to complete */
 	for (i=0; i<(int)(max_wait/50); ++i) {
@@ -1363,5 +1370,6 @@
 		continue;
 
-	    if (pjsua_var.acc[i].regc) {
+	    if (pjsua_var.acc[i].regc && (flags & PJSUA_DESTROY_NO_NETWORK)==0)
+	    {
 		pjsua_acc_set_registration(i, PJ_FALSE);
 	    }
@@ -1388,4 +1396,9 @@
 	}
 	
+	/* No need to wait if we didn't send anything */
+	if (flags & PJSUA_DESTROY_NO_NETWORK) {
+	    max_wait = 0;
+	}
+
 	/* Second stage, wait for unregistrations to complete */
 	for (i=0; i<(int)(max_wait/50); ++i) {
@@ -1408,6 +1421,7 @@
 	 * transports shutdown to complete: 
 	 */
-	if (i < 20)
+	if (i < 20 && (flags & PJSUA_DESTROY_NO_NETWORK) == 0) {
 	    busy_sleep(1000 - i*50);
+	}
 
 	PJ_LOG(4,(THIS_FILE, "Destroying..."));
@@ -1467,4 +1481,10 @@
     /* Done. */
     return PJ_SUCCESS;
+}
+
+
+PJ_DEF(pj_status_t) pjsua_destroy(void)
+{
+    return pjsua_destroy2(0);
 }
 
Index: /pjproject/branches/1.x/pjsip/src/pjsua-lib/pjsua_media.c
===================================================================
--- /pjproject/branches/1.x/pjsip/src/pjsua-lib/pjsua_media.c	(revision 3828)
+++ /pjproject/branches/1.x/pjsip/src/pjsua-lib/pjsua_media.c	(revision 3829)
@@ -659,5 +659,5 @@
  * Destroy pjsua media subsystem.
  */
-pj_status_t pjsua_media_subsys_destroy(void)
+pj_status_t pjsua_media_subsys_destroy(unsigned flags)
 {
     unsigned i;
@@ -699,4 +699,8 @@
 	}
 	if (pjsua_var.calls[i].med_tp && pjsua_var.calls[i].med_tp_auto_del) {
+	    /* TODO: check if we're not allowed to send to network in the
+	     *       "flags", and if so do not do TURN allocation...
+	     */
+	    PJ_UNUSED_ARG(flags);
 	    pjmedia_transport_close(pjsua_var.calls[i].med_tp);
 	}
Index: /pjproject/branches/1.x/pjsip/src/pjsua-lib/pjsua_pres.c
===================================================================
--- /pjproject/branches/1.x/pjsip/src/pjsua-lib/pjsua_pres.c	(revision 3828)
+++ /pjproject/branches/1.x/pjsip/src/pjsua-lib/pjsua_pres.c	(revision 3829)
@@ -1278,5 +1278,5 @@
 
 /* Unpublish presence publication */
-void pjsua_pres_unpublish(pjsua_acc *acc)
+void pjsua_pres_unpublish(pjsua_acc *acc, unsigned flags)
 {
     if (acc->publish_sess) {
@@ -1284,5 +1284,9 @@
 
 	acc->online_status = PJ_FALSE;
-	send_publish(acc->index, PJ_FALSE);
+
+	if ((flags & PJSUA_DESTROY_NO_NETWORK) == 0) {
+	    send_publish(acc->index, PJ_FALSE);
+	}
+
 	/* By ticket #364, don't destroy the session yet (let the callback
 	   destroy it)
@@ -1297,5 +1301,5 @@
 
 /* Terminate server subscription for the account */
-void pjsua_pres_delete_acc(int acc_id)
+void pjsua_pres_delete_acc(int acc_id, unsigned flags)
 {
     pjsua_acc *acc = &pjsua_var.acc[acc_id];
@@ -1319,9 +1323,13 @@
 	pjsip_pres_set_status(uapres->sub, &pres_status);
 
-	if (pjsip_pres_notify(uapres->sub, 
-			      PJSIP_EVSUB_STATE_TERMINATED, NULL,
-			      &reason, &tdata)==PJ_SUCCESS)
-	{
-	    pjsip_pres_send_request(uapres->sub, tdata);
+	if ((flags & PJSUA_DESTROY_NO_NETWORK) == 0) {
+	    if (pjsip_pres_notify(uapres->sub,
+				  PJSIP_EVSUB_STATE_TERMINATED, NULL,
+				  &reason, &tdata)==PJ_SUCCESS)
+	    {
+		pjsip_pres_send_request(uapres->sub, tdata);
+	    }
+	} else {
+	    pjsip_pres_terminate(uapres->sub, PJ_FALSE);
 	}
 
@@ -1334,5 +1342,5 @@
 
     /* Terminate presence publication, if any */
-    pjsua_pres_unpublish(acc);
+    pjsua_pres_unpublish(acc, flags);
 }
 
@@ -2253,5 +2261,5 @@
  * Shutdown presence.
  */
-void pjsua_pres_shutdown(void)
+void pjsua_pres_shutdown(unsigned flags)
 {
     unsigned i;
@@ -2267,5 +2275,5 @@
 	if (!pjsua_var.acc[i].valid)
 	    continue;
-	pjsua_pres_delete_acc(i);
+	pjsua_pres_delete_acc(i, flags);
     }
 
@@ -2274,9 +2282,11 @@
     }
 
-    refresh_client_subscriptions();
-
-    for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
-	if (pjsua_var.acc[i].valid)
-	    pjsua_pres_update_acc(i, PJ_FALSE);
-    }
-}
+    if ((flags & PJSUA_DESTROY_NO_NETWORK) == 0) {
+	refresh_client_subscriptions();
+
+	for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
+	    if (pjsua_var.acc[i].valid)
+		pjsua_pres_update_acc(i, PJ_FALSE);
+	}
+    }
+}
