Index: pjproject/trunk/pjlib/include/pj/ssl_sock.h
===================================================================
--- pjproject/trunk/pjlib/include/pj/ssl_sock.h	(revision 5884)
+++ pjproject/trunk/pjlib/include/pj/ssl_sock.h	(revision 5885)
@@ -606,5 +606,6 @@
     /**
      * This callback is called when new connection arrives as the result
-     * of pj_ssl_sock_start_accept().
+     * of pj_ssl_sock_start_accept(). If the status of accept operation is
+     * needed use on_accept_complete2 instead of this callback.
      *
      * @param ssock	The secure socket.
@@ -622,4 +623,27 @@
 				    const pj_sockaddr_t *src_addr,
 				    int src_addr_len);
+    /**
+     * This callback is called when new connection arrives as the result
+     * of pj_ssl_sock_start_accept().
+     *
+     * @param asock	The active socket.
+     * @param newsock	The new incoming socket.
+     * @param src_addr	The source address of the connection.
+     * @param addr_len	Length of the source address.
+     * @param status	The status of the accept operation. This may contain
+     *			non-PJ_SUCCESS for example when the TCP listener is in
+     *			bad state for example on iOS platform after the
+     *			application waking up from background.
+     *
+     * @return		PJ_TRUE if further accept() is desired, and PJ_FALSE
+     *			when application no longer wants to accept incoming
+     *			connection. Application may destroy the active socket
+     *			in the callback and return PJ_FALSE here.
+     */
+    pj_bool_t (*on_accept_complete2)(pj_ssl_sock_t *ssock,
+				     pj_ssl_sock_t *newsock,
+				     const pj_sockaddr_t *src_addr,
+				     int src_addr_len, 
+				     pj_status_t status);
 
     /**
Index: pjproject/trunk/pjlib/src/pj/ssl_sock_ossl.c
===================================================================
--- pjproject/trunk/pjlib/src/pj/ssl_sock_ossl.c	(revision 5884)
+++ pjproject/trunk/pjlib/src/pj/ssl_sock_ossl.c	(revision 5885)
@@ -1710,4 +1710,11 @@
 		      errmsg));
 
+	    if (*ssock->param.cb.on_accept_complete2) {
+		(*ssock->param.cb.on_accept_complete2) 
+		      (ssock->parent, ssock, (pj_sockaddr_t*)&ssock->rem_addr, 
+		      pj_sockaddr_get_len((pj_sockaddr_t*)&ssock->rem_addr), 
+		      status);
+	    }
+
 	    /* Originally, this is a workaround for ticket #985. However,
 	     * a race condition may occur in multiple worker threads
@@ -1749,5 +1756,13 @@
 	}
 	/* Notify application the newly accepted SSL socket */
-	if (ssock->param.cb.on_accept_complete) {
+	if (ssock->param.cb.on_accept_complete2) {
+	    pj_bool_t ret;
+	    ret = (*ssock->param.cb.on_accept_complete2) 
+		    (ssock->parent, ssock, (pj_sockaddr_t*)&ssock->rem_addr, 
+		    pj_sockaddr_get_len((pj_sockaddr_t*)&ssock->rem_addr), 
+		    status);
+	    if (ret == PJ_FALSE)
+		return PJ_FALSE;	
+	} else if (ssock->param.cb.on_accept_complete) {
 	    pj_bool_t ret;
 	    ret = (*ssock->param.cb.on_accept_complete)
@@ -2520,4 +2535,27 @@
 
 
+static pj_bool_t asock_on_accept_complete2(pj_activesock_t *asock,
+					   pj_sock_t newsock,
+					   const pj_sockaddr_t *src_addr,
+					   int src_addr_len,
+					   pj_status_t status)
+{
+    pj_bool_t ret = PJ_TRUE;
+    if (status != PJ_SUCCESS) {
+	pj_ssl_sock_t *ssock = (pj_ssl_sock_t*)
+			pj_activesock_get_user_data(asock);
+
+	if (ssock->param.cb.on_accept_complete2) {
+	    (*ssock->param.cb.on_accept_complete2) (ssock->parent, ssock, 
+						    src_addr, src_addr_len, 
+						    status);
+	}
+    } else {
+	ret = asock_on_accept_complete(asock, newsock, src_addr, src_addr_len);
+    }
+    return ret;
+}
+
+
 static pj_bool_t asock_on_connect_complete (pj_activesock_t *asock,
 					    pj_status_t status)
@@ -3426,5 +3464,6 @@
 
     pj_bzero(&asock_cb, sizeof(asock_cb));
-    asock_cb.on_accept_complete = asock_on_accept_complete;
+    //asock_cb.on_accept_complete = asock_on_accept_complete;
+    asock_cb.on_accept_complete2 = asock_on_accept_complete2;
 
     status = pj_activesock_create(pool,
Index: pjproject/trunk/pjsip/include/pjsip/sip_transport_tls.h
===================================================================
--- pjproject/trunk/pjsip/include/pjsip/sip_transport_tls.h	(revision 5884)
+++ pjproject/trunk/pjsip/include/pjsip/sip_transport_tls.h	(revision 5885)
@@ -76,4 +76,20 @@
 
 /**
+ * This structure describe the parameter passed from #on_accept_fail_cb().
+ */
+typedef struct pjsip_tls_on_accept_fail_param {
+    /**
+     * Remote address of the fail accept operation of the TLS listener.
+     */
+    const pj_sockaddr_t *remote_addr;
+
+    /**
+     * Error status of the fail accept operation of the TLS listener.
+     */
+    pj_status_t status;
+
+} pjsip_tls_on_accept_fail_param;
+
+/**
  * TLS transport settings.
  */
@@ -307,4 +323,11 @@
      */
     pj_bool_t sockopt_ignore_error;
+
+    /**
+     * Callback to be called when a accept operation of the TLS listener fails.
+     *
+     * @param param         The parameter to the callback.
+     */
+    void(*on_accept_fail_cb)(const pjsip_tls_on_accept_fail_param *param);
 
 } pjsip_tls_setting;
Index: pjproject/trunk/pjsip/src/pjsip/sip_transport_tls.c
===================================================================
--- pjproject/trunk/pjsip/src/pjsip/sip_transport_tls.c	(revision 5884)
+++ pjproject/trunk/pjsip/src/pjsip/sip_transport_tls.c	(revision 5885)
@@ -122,8 +122,9 @@
 
 /* This callback is called when pending accept() operation completes. */
-static pj_bool_t on_accept_complete(pj_ssl_sock_t *ssock,
+static pj_bool_t on_accept_complete2(pj_ssl_sock_t *ssock,
 				    pj_ssl_sock_t *new_ssock,
 				    const pj_sockaddr_t *src_addr,
-				    int src_addr_len);
+				    int src_addr_len,
+				    pj_status_t status);
 
 /* Callback on incoming data */
@@ -295,5 +296,5 @@
     pj_ssl_sock_param_default(ssock_param);
     ssock_param->sock_af = af;
-    ssock_param->cb.on_accept_complete = &on_accept_complete;
+    ssock_param->cb.on_accept_complete2 = &on_accept_complete2;
     ssock_param->async_cnt = listener->async_cnt;
     ssock_param->ioqueue = pjsip_endpt_get_ioqueue(listener->endpt);
@@ -1301,13 +1302,10 @@
 
 
-/*
- * This callback is called by SSL socket when pending accept() operation
- * has completed.
- */
-static pj_bool_t on_accept_complete(pj_ssl_sock_t *ssock,
-				    pj_ssl_sock_t *new_ssock,
-				    const pj_sockaddr_t *src_addr,
-				    int src_addr_len)
-{
+static pj_bool_t on_accept_complete2(pj_ssl_sock_t *ssock,
+				     pj_ssl_sock_t *new_ssock,
+				     const pj_sockaddr_t *src_addr,
+				     int src_addr_len, 
+				     pj_status_t accept_status)
+{    
     struct tls_listener *listener;
     struct tls_transport *tls;
@@ -1324,8 +1322,28 @@
     listener = (struct tls_listener*) pj_ssl_sock_get_user_data(ssock);
 
+    if (accept_status != PJ_SUCCESS) {
+	if (listener->tls_setting.on_accept_fail_cb) {
+	    pjsip_tls_on_accept_fail_param param;
+	    pj_bzero(&param, sizeof(param));
+	    param.status = accept_status;
+	    param.remote_addr = src_addr;
+	    (*listener->tls_setting.on_accept_fail_cb) (&param);
+	}
+
+	return PJ_FALSE;
+    }
+
     PJ_ASSERT_RETURN(new_ssock, PJ_TRUE);
 
-    if (!listener->is_registered)
+    if (!listener->is_registered) {
+	if (listener->tls_setting.on_accept_fail_cb) {
+	    pjsip_tls_on_accept_fail_param param;
+	    pj_bzero(&param, sizeof(param));
+	    param.status = PJSIP_TLS_EACCEPT;
+	    param.remote_addr = src_addr;
+	    (*listener->tls_setting.on_accept_fail_cb) (&param);
+	}
 	return PJ_FALSE;
+    }	
 
     PJ_LOG(4,(listener->factory.obj_name, 
@@ -1344,4 +1362,12 @@
     if (status != PJ_SUCCESS) {
 	pj_ssl_sock_close(new_ssock);
+
+	if (listener->tls_setting.on_accept_fail_cb) {
+	    pjsip_tls_on_accept_fail_param param;
+	    pj_bzero(&param, sizeof(param));
+	    param.status = status;
+	    param.remote_addr = src_addr;
+	    (*listener->tls_setting.on_accept_fail_cb) (&param);
+	}
 	return PJ_TRUE;
     }
@@ -1358,6 +1384,14 @@
 			 &ssl_info.local_addr, &tmp_src_addr, NULL, &tls);
     
-    if (status != PJ_SUCCESS)
+    if (status != PJ_SUCCESS) {
+	if (listener->tls_setting.on_accept_fail_cb) {
+	    pjsip_tls_on_accept_fail_param param;
+	    pj_bzero(&param, sizeof(param));
+	    param.status = status;
+	    param.remote_addr = src_addr;
+	    (*listener->tls_setting.on_accept_fail_cb) (&param);
+	}
 	return PJ_TRUE;
+    }
 
     /* Set the "pending" SSL socket user data */
@@ -1440,4 +1474,16 @@
     return PJ_TRUE;
 }
+
+/*
+ * This callback is called by SSL socket when pending accept() operation
+ * has completed.
+ */
+//static pj_bool_t on_accept_complete(pj_ssl_sock_t *ssock,
+//				    pj_ssl_sock_t *new_ssock,
+//				    const pj_sockaddr_t *src_addr,
+//				    int src_addr_len)
+//{
+//    PJ_UNUSED_ARG(src_addr_len);
+//}
 
 
