Index: pjproject/trunk/pjsip/src/pjsip/sip_transport_tcp.c
===================================================================
--- pjproject/trunk/pjsip/src/pjsip/sip_transport_tcp.c	(revision 582)
+++ pjproject/trunk/pjsip/src/pjsip/sip_transport_tcp.c	(revision 600)
@@ -54,4 +54,6 @@
     pj_ioqueue_op_key_t	     op_key;
     struct tcp_listener	    *listener;
+    unsigned		     index;
+    pj_pool_t		    *pool;
     pj_sock_t		     new_sock;
     int			     addr_len;
@@ -75,5 +77,5 @@
     pj_ioqueue_key_t	    *key;
     unsigned		     async_cnt;
-    struct pending_accept    accept_op[MAX_ASYNC_CNT];
+    struct pending_accept   *accept_op[MAX_ASYNC_CNT];
 };
 
@@ -144,4 +146,5 @@
 /* Common function to create and initialize transport */
 static pj_status_t tcp_create(struct tcp_listener *listener,
+			      pj_pool_t *pool,
 			      pj_sock_t sock, pj_bool_t is_server,
 			      const pj_sockaddr_in *local,
@@ -298,9 +301,21 @@
 
     for (i=0; i<async_cnt; ++i) {
-	pj_ioqueue_op_key_init(&listener->accept_op[i].op_key, 
-				sizeof(listener->accept_op[i].op_key));
-	listener->accept_op[i].listener = listener;
-
-	on_accept_complete(listener->key, &listener->accept_op[i].op_key,
+	pj_pool_t *pool;
+
+	pool = pjsip_endpt_create_pool(endpt, "tcps%p", POOL_TP_INIT, 
+				       POOL_TP_INIT);
+	if (!pool) {
+	    status = PJ_ENOMEM;
+	    goto on_error;
+	}
+
+	listener->accept_op[i] = pj_pool_zalloc(pool, 
+						sizeof(struct pending_accept));
+	pj_ioqueue_op_key_init(&listener->accept_op[i]->op_key, 
+				sizeof(listener->accept_op[i]->op_key));
+	listener->accept_op[i]->listener = listener;
+	listener->accept_op[i]->index = i;
+
+	on_accept_complete(listener->key, &listener->accept_op[i]->op_key,
 			   listener->sock, PJ_EPENDING);
     }
@@ -326,4 +341,5 @@
 {
     struct tcp_listener *listener = (struct tcp_listener *)factory;
+    unsigned i;
 
     if (listener->is_registered) {
@@ -346,4 +362,11 @@
 	pj_lock_destroy(listener->factory.lock);
 	listener->factory.lock = NULL;
+    }
+
+    for (i=0; i<PJ_ARRAY_SIZE(listener->accept_op); ++i) {
+	if (listener->accept_op[i] && listener->accept_op[i]->pool) {
+	    pj_pool_release(listener->accept_op[i]->pool);
+	    listener->accept_op[i]->pool = NULL;
+	}
     }
 
@@ -409,4 +432,5 @@
  */
 static pj_status_t tcp_create( struct tcp_listener *listener,
+			       pj_pool_t *pool,
 			       pj_sock_t sock, pj_bool_t is_server,
 			       const pj_sockaddr_in *local,
@@ -415,5 +439,4 @@
 {
     struct tcp_transport *tcp;
-    pj_pool_t *pool;
     pj_ioqueue_t *ioqueue;
     pj_ioqueue_callback tcp_callback;
@@ -424,8 +447,9 @@
 
 
-    pool = pjsip_endpt_create_pool(listener->endpt, "tcp",
-				   POOL_TP_INIT, POOL_TP_INC);
-    PJ_ASSERT_RETURN(pool != NULL, PJ_ENOMEM);
-    
+    if (pool == NULL) {
+	pool = pjsip_endpt_create_pool(listener->endpt, "tcp",
+				       POOL_TP_INIT, POOL_TP_INC);
+	PJ_ASSERT_RETURN(pool != NULL, PJ_ENOMEM);
+    }    
 
     /*
@@ -684,5 +708,6 @@
 			     PJ_IOQUEUE_ALWAYS_ASYNC);
     if (status != PJ_SUCCESS && status != PJ_EPENDING) {
-	tcp_perror(tcp->base.obj_name, "ioqueue recv() error", status);
+	PJ_LOG(4, (tcp->base.obj_name, "ioqueue recv() error, status=%d", 
+		   status));
 	return status;
     }
@@ -745,5 +770,5 @@
 
     /* Create the transport descriptor */
-    status = tcp_create(listener, sock, PJ_FALSE, &local_addr, 
+    status = tcp_create(listener, NULL, sock, PJ_FALSE, &local_addr, 
 			(pj_sockaddr_in*)rem_addr, &tcp);
     if (status != PJ_SUCCESS)
@@ -846,10 +871,14 @@
 
 	} else {
+	    pj_pool_t *pool;
+	    struct pending_accept *new_op;
 
 	    if (sock == PJ_INVALID_SOCKET) {
 		sock = accept_op->new_sock;
-		PJ_LOG(4,(listener->obj_name, 
-		          "Warning: ioqueue reports -1 in on_accept_complete()"
-			  " sock argument"));
+	    }
+
+	    if (sock == PJ_INVALID_SOCKET) {
+		pj_assert(!"Should not happen. status should be error");
+		goto next_accept;
 	    }
 
@@ -864,9 +893,19 @@
 		      sock));
 
+	    /* Create new accept_opt */
+	    pool = pjsip_endpt_create_pool(listener->endpt, "tcps%p", 
+					   POOL_TP_INIT, POOL_TP_INC);
+	    new_op = pj_pool_zalloc(pool, sizeof(struct pending_accept));
+	    new_op->pool = pool;
+	    new_op->listener = listener;
+	    new_op->index = accept_op->index;
+	    pj_ioqueue_op_key_init(&new_op->op_key, sizeof(new_op->op_key));
+	    listener->accept_op[accept_op->index] = new_op;
+
 	    /* 
 	     * Incoming connections!
 	     * Create TCP transport for the new socket.
 	     */
-	    status = tcp_create( listener, sock, PJ_TRUE,
+	    status = tcp_create( listener, accept_op->pool, sock, PJ_TRUE,
 				 &accept_op->local_addr, 
 				 &accept_op->remote_addr, &tcp);
@@ -878,6 +917,9 @@
 		}
 	    }
+
+	    accept_op = new_op;
 	}
 
+next_accept:
 	/*
 	 * Start the next asynchronous accept() operation.
@@ -1129,8 +1171,5 @@
 	{
 
-	    /* Report error to endpoint. */
-	    PJSIP_ENDPT_LOG_ERROR((rdata->tp_info.transport->endpt,
-				   rdata->tp_info.transport->obj_name,
-				   -bytes_read, "TCP recv() error"));
+	    /* Socket error. */
 
 	    /* We can not destroy the transport since high level objects may
@@ -1173,8 +1212,5 @@
 
 	} else {
-	    /* Report error to endpoint */
-	    PJSIP_ENDPT_LOG_ERROR((rdata->tp_info.transport->endpt,
-				   rdata->tp_info.transport->obj_name,
-				   status, "tcp recv() error"));
+	    /* Socket error */
 
 	    /* We can not destroy the transport since high level objects may
