Index: /pjproject/trunk/pjlib/src/pj/ioqueue_common_abs.c
===================================================================
--- /pjproject/trunk/pjlib/src/pj/ioqueue_common_abs.c	(revision 591)
+++ /pjproject/trunk/pjlib/src/pj/ioqueue_common_abs.c	(revision 592)
@@ -197,6 +197,6 @@
 	h->connecting = 0;
 
-        ioqueue_remove_from_set(ioqueue, h->fd, WRITEABLE_EVENT);
-        ioqueue_remove_from_set(ioqueue, h->fd, EXCEPTION_EVENT);
+        ioqueue_remove_from_set(ioqueue, h, WRITEABLE_EVENT);
+        ioqueue_remove_from_set(ioqueue, h, EXCEPTION_EVENT);
 
 
@@ -273,5 +273,5 @@
 
             if (pj_list_empty(&h->write_list))
-                ioqueue_remove_from_set(ioqueue, h->fd, WRITEABLE_EVENT);
+                ioqueue_remove_from_set(ioqueue, h, WRITEABLE_EVENT);
 
         }
@@ -326,5 +326,5 @@
                 /* Clear operation if there's no more data to send. */
                 if (pj_list_empty(&h->write_list))
-                    ioqueue_remove_from_set(ioqueue, h->fd, WRITEABLE_EVENT);
+                    ioqueue_remove_from_set(ioqueue, h, WRITEABLE_EVENT);
 
             }
@@ -379,5 +379,5 @@
 	/* Clear bit in fdset if there is no more pending accept */
         if (pj_list_empty(&h->accept_list))
-            ioqueue_remove_from_set(ioqueue, h->fd, READABLE_EVENT);
+            ioqueue_remove_from_set(ioqueue, h, READABLE_EVENT);
 
 	rc=pj_sock_accept(h->fd, accept_op->accept_fd, 
@@ -412,5 +412,5 @@
         /* Clear fdset if there is no pending read. */
         if (pj_list_empty(&h->read_list))
-            ioqueue_remove_from_set(ioqueue, h->fd, READABLE_EVENT);
+            ioqueue_remove_from_set(ioqueue, h, READABLE_EVENT);
 
         bytes_read = read_op->size;
@@ -519,6 +519,6 @@
     h->connecting = 0;
 
-    ioqueue_remove_from_set(ioqueue, h->fd, WRITEABLE_EVENT);
-    ioqueue_remove_from_set(ioqueue, h->fd, EXCEPTION_EVENT);
+    ioqueue_remove_from_set(ioqueue, h, WRITEABLE_EVENT);
+    ioqueue_remove_from_set(ioqueue, h, EXCEPTION_EVENT);
 
     pj_mutex_unlock(h->mutex);
@@ -586,5 +586,5 @@
     pj_mutex_lock(key->mutex);
     pj_list_insert_before(&key->read_list, read_op);
-    ioqueue_add_to_set(key->ioqueue, key->fd, READABLE_EVENT);
+    ioqueue_add_to_set(key->ioqueue, key, READABLE_EVENT);
     pj_mutex_unlock(key->mutex);
 
@@ -654,5 +654,5 @@
     pj_mutex_lock(key->mutex);
     pj_list_insert_before(&key->read_list, read_op);
-    ioqueue_add_to_set(key->ioqueue, key->fd, READABLE_EVENT);
+    ioqueue_add_to_set(key->ioqueue, key, READABLE_EVENT);
     pj_mutex_unlock(key->mutex);
 
@@ -760,5 +760,5 @@
     pj_mutex_lock(key->mutex);
     pj_list_insert_before(&key->write_list, write_op);
-    ioqueue_add_to_set(key->ioqueue, key->fd, WRITEABLE_EVENT);
+    ioqueue_add_to_set(key->ioqueue, key, WRITEABLE_EVENT);
     pj_mutex_unlock(key->mutex);
 
@@ -828,4 +828,5 @@
 	    status = status;
         }
+	PJ_LOG(3,(THIS_FILE, "pending write operation!!"));
     }
 
@@ -877,5 +878,5 @@
     pj_mutex_lock(key->mutex);
     pj_list_insert_before(&key->write_list, write_op);
-    ioqueue_add_to_set(key->ioqueue, key->fd, WRITEABLE_EVENT);
+    ioqueue_add_to_set(key->ioqueue, key, WRITEABLE_EVENT);
     pj_mutex_unlock(key->mutex);
 
@@ -946,5 +947,5 @@
     pj_mutex_lock(key->mutex);
     pj_list_insert_before(&key->accept_list, accept_op);
-    ioqueue_add_to_set(key->ioqueue, key->fd, READABLE_EVENT);
+    ioqueue_add_to_set(key->ioqueue, key, READABLE_EVENT);
     pj_mutex_unlock(key->mutex);
 
@@ -982,6 +983,6 @@
             pj_mutex_lock(key->mutex);
 	    key->connecting = PJ_TRUE;
-            ioqueue_add_to_set(key->ioqueue, key->fd, WRITEABLE_EVENT);
-            ioqueue_add_to_set(key->ioqueue, key->fd, EXCEPTION_EVENT);
+            ioqueue_add_to_set(key->ioqueue, key, WRITEABLE_EVENT);
+            ioqueue_add_to_set(key->ioqueue, key, EXCEPTION_EVENT);
             pj_mutex_unlock(key->mutex);
 	    return PJ_EPENDING;
Index: /pjproject/trunk/pjlib/src/pj/ioqueue_common_abs.h
===================================================================
--- /pjproject/trunk/pjlib/src/pj/ioqueue_common_abs.h	(revision 591)
+++ /pjproject/trunk/pjlib/src/pj/ioqueue_common_abs.h	(revision 592)
@@ -129,8 +129,8 @@
 
 static void ioqueue_add_to_set( pj_ioqueue_t *ioqueue,
-                                pj_sock_t fd,
+                                pj_ioqueue_key_t *key,
                                 enum ioqueue_event_type event_type );
 static void ioqueue_remove_from_set( pj_ioqueue_t *ioqueue,
-                                     pj_sock_t fd, 
+                                     pj_ioqueue_key_t *key, 
                                      enum ioqueue_event_type event_type);
 
Index: /pjproject/trunk/pjlib/src/pj/ioqueue_epoll.c
===================================================================
--- /pjproject/trunk/pjlib/src/pj/ioqueue_epoll.c	(revision 591)
+++ /pjproject/trunk/pjlib/src/pj/ioqueue_epoll.c	(revision 592)
@@ -142,6 +142,6 @@
 #define THIS_FILE   "ioq_epoll"
 
-#define TRACE_(expr) PJ_LOG(3,expr)
-//#define TRACE_(expr)
+//#define TRACE_(expr) PJ_LOG(3,expr)
+#define TRACE_(expr)
 
 /*
@@ -156,4 +156,10 @@
 {
     DECLARE_COMMON_KEY
+};
+
+struct queue
+{
+    pj_ioqueue_key_t	    *key;
+    enum ioqueue_event_type  event_type;
 };
 
@@ -168,4 +174,6 @@
     pj_ioqueue_key_t	hlist;
     int			epfd;
+    struct epoll_event *events;
+    struct queue       *queue;
 };
 
@@ -230,4 +238,10 @@
     }
     
+    ioqueue->events = pj_pool_calloc(pool, max_fd, sizeof(struct epoll_event));
+    PJ_ASSERT_RETURN(ioqueue->events != NULL, PJ_ENOMEM);
+
+    ioqueue->queue = pj_pool_calloc(pool, max_fd, sizeof(struct queue));
+    PJ_ASSERT_RETURN(ioqueue->queue != NULL, PJ_ENOMEM);
+
     PJ_LOG(4, ("pjlib", "epoll I/O Queue created (%p)", ioqueue));
 
@@ -306,5 +320,5 @@
 
     /* os_epoll_ctl. */
-    ev.events = EPOLLIN | EPOLLOUT | EPOLLERR;
+    ev.events = EPOLLIN | EPOLLERR;
     ev.epoll_data = (epoll_data_type)key;
     status = os_epoll_ctl(ioqueue->epfd, EPOLL_CTL_ADD, sock, &ev);
@@ -323,4 +337,6 @@
     ++ioqueue->count;
 
+    //TRACE_((THIS_FILE, "socket registered, count=%d", ioqueue->count));
+
 on_return:
     *p_key = key;
@@ -374,7 +390,14 @@
  */
 static void ioqueue_remove_from_set( pj_ioqueue_t *ioqueue,
-                                     pj_sock_t fd, 
+                                     pj_ioqueue_key_t *key, 
                                      enum ioqueue_event_type event_type)
 {
+    if (event_type == WRITEABLE_EVENT) {
+	struct epoll_event ev;
+
+	ev.events = EPOLLIN | EPOLLERR;
+	ev.epoll_data = (epoll_data_type)key;
+	os_epoll_ctl( ioqueue->epfd, EPOLL_CTL_MOD, key->fd, &ev);
+    }	
 }
 
@@ -386,7 +409,14 @@
  */
 static void ioqueue_add_to_set( pj_ioqueue_t *ioqueue,
-                                pj_sock_t fd,
+                                pj_ioqueue_key_t *key,
                                 enum ioqueue_event_type event_type )
 {
+    if (event_type == WRITEABLE_EVENT) {
+	struct epoll_event ev;
+
+	ev.events = EPOLLIN | EPOLLOUT | EPOLLERR;
+	ev.epoll_data = (epoll_data_type)key;
+	os_epoll_ctl( ioqueue->epfd, EPOLL_CTL_MOD, key->fd, &ev);
+    }	
 }
 
@@ -398,20 +428,29 @@
 {
     int i, count, processed;
-    struct epoll_event events[PJ_IOQUEUE_MAX_EVENTS_IN_SINGLE_POLL];
     int msec;
-    struct queue {
-	pj_ioqueue_key_t	*key;
-	enum ioqueue_event_type	 event_type;
-    } queue[PJ_IOQUEUE_MAX_EVENTS_IN_SINGLE_POLL];
+    struct epoll_event *events = ioqueue->events;
+    struct queue *queue = ioqueue->queue;
+    pj_timestamp t1, t2;
     
     PJ_CHECK_STACK();
 
     msec = timeout ? PJ_TIME_VAL_MSEC(*timeout) : 9000;
-    
-    count = os_epoll_wait( ioqueue->epfd, events, PJ_ARRAY_SIZE(events), msec);
-    if (count == 0)
+
+    TRACE_((THIS_FILE, "start os_epoll_wait, msec=%d", msec));
+    pj_get_timestamp(&t1);
+ 
+    count = os_epoll_wait( ioqueue->epfd, events, ioqueue->max, msec);
+    if (count == 0) {
+	TRACE_((THIS_FILE, "os_epoll_wait timed out"));
 	return count;
-    else if (count < 0)
+    }
+    else if (count < 0) {
+	TRACE_((THIS_FILE, "os_epoll_wait error"));
 	return -pj_get_netos_error();
+    }
+
+    pj_get_timestamp(&t2);
+    TRACE_((THIS_FILE, "os_epoll_wait returns %d, time=%d usec",
+		       count, pj_elapsed_usec(&t1, &t2)));
 
     /* Lock ioqueue. */
@@ -421,4 +460,6 @@
 	pj_ioqueue_key_t *h = (pj_ioqueue_key_t*)(epoll_data_type)
 				events[i].epoll_data;
+
+	TRACE_((THIS_FILE, "event %d: events=%d", i, events[i].events));
 
 	/*
@@ -487,4 +528,9 @@
 	pj_thread_sleep(msec);
     }
+
+    pj_get_timestamp(&t1);
+    TRACE_((THIS_FILE, "ioqueue_poll() returns %d, time=%d usec",
+		       processed, pj_elapsed_usec(&t2, &t1)));
+
     return processed;
 }
Index: /pjproject/trunk/pjlib/src/pj/ioqueue_select.c
===================================================================
--- /pjproject/trunk/pjlib/src/pj/ioqueue_select.c	(revision 591)
+++ /pjproject/trunk/pjlib/src/pj/ioqueue_select.c	(revision 592)
@@ -543,5 +543,5 @@
  */
 static void ioqueue_remove_from_set( pj_ioqueue_t *ioqueue,
-                                     pj_sock_t fd, 
+                                     pj_ioqueue_key_t *key, 
                                      enum ioqueue_event_type event_type)
 {
@@ -549,9 +549,9 @@
 
     if (event_type == READABLE_EVENT)
-        PJ_FD_CLR((pj_sock_t)fd, &ioqueue->rfdset);
+        PJ_FD_CLR((pj_sock_t)key->fd, &ioqueue->rfdset);
     else if (event_type == WRITEABLE_EVENT)
-        PJ_FD_CLR((pj_sock_t)fd, &ioqueue->wfdset);
+        PJ_FD_CLR((pj_sock_t)key->fd, &ioqueue->wfdset);
     else if (event_type == EXCEPTION_EVENT)
-        PJ_FD_CLR((pj_sock_t)fd, &ioqueue->xfdset);
+        PJ_FD_CLR((pj_sock_t)key->fd, &ioqueue->xfdset);
     else
         pj_assert(0);
@@ -567,5 +567,5 @@
  */
 static void ioqueue_add_to_set( pj_ioqueue_t *ioqueue,
-                                pj_sock_t fd,
+                                pj_ioqueue_key_t *key,
                                 enum ioqueue_event_type event_type )
 {
@@ -573,9 +573,9 @@
 
     if (event_type == READABLE_EVENT)
-        PJ_FD_SET((pj_sock_t)fd, &ioqueue->rfdset);
+        PJ_FD_SET((pj_sock_t)key->fd, &ioqueue->rfdset);
     else if (event_type == WRITEABLE_EVENT)
-        PJ_FD_SET((pj_sock_t)fd, &ioqueue->wfdset);
+        PJ_FD_SET((pj_sock_t)key->fd, &ioqueue->wfdset);
     else if (event_type == EXCEPTION_EVENT)
-        PJ_FD_SET((pj_sock_t)fd, &ioqueue->xfdset);
+        PJ_FD_SET((pj_sock_t)key->fd, &ioqueue->xfdset);
     else
         pj_assert(0);
Index: /pjproject/trunk/pjlib/src/pjlib-test/ioq_tcp.c
===================================================================
--- /pjproject/trunk/pjlib/src/pjlib-test/ioq_tcp.c	(revision 591)
+++ /pjproject/trunk/pjlib/src/pjlib-test/ioq_tcp.c	(revision 592)
@@ -39,5 +39,4 @@
 
 #define THIS_FILE	    "test_tcp"
-#define PORT		    50000
 #define NON_EXISTANT_PORT   50123
 #define LOOP		    100
@@ -258,11 +257,18 @@
 
     // Bind server socket.
-    memset(&addr, 0, sizeof(addr));
-    addr.sin_family = PJ_AF_INET;
-    addr.sin_port = pj_htons(PORT);
-    if (pj_sock_bind(ssock, &addr, sizeof(addr))) {
+    pj_sockaddr_in_init(&addr, 0, 0);
+    if ((rc=pj_sock_bind(ssock, &addr, sizeof(addr))) != 0 ) {
         app_perror("...bind error", rc);
 	status=-10; goto on_error;
     }
+
+    // Get server address.
+    client_addr_len = sizeof(addr);
+    rc = pj_sock_getsockname(ssock, &addr, &client_addr_len);
+    if (rc != PJ_SUCCESS) {
+        app_perror("...ERROR in pj_sock_getsockname()", rc);
+	status=-15; goto on_error;
+    }
+    addr.sin_addr = pj_inet_addr(pj_cstr(&s, "127.0.0.1"));
 
     // Create I/O Queue.
@@ -302,10 +308,4 @@
 	++pending_op;
     }
-
-    // Initialize remote address.
-    memset(&addr, 0, sizeof(addr));
-    addr.sin_family = PJ_AF_INET;
-    addr.sin_port = pj_htons(PORT);
-    addr.sin_addr = pj_inet_addr(pj_cstr(&s, "127.0.0.1"));
 
     // Client socket connect()
@@ -462,8 +462,5 @@
 
     // Initialize remote address.
-    memset(&addr, 0, sizeof(addr));
-    addr.sin_family = PJ_AF_INET;
-    addr.sin_port = pj_htons(NON_EXISTANT_PORT);
-    addr.sin_addr = pj_inet_addr(pj_cstr(&s, "127.0.0.1"));
+    pj_sockaddr_in_init(&addr, pj_cstr(&s, "127.0.0.1"), NON_EXISTANT_PORT);
 
     // Client socket connect()
Index: /pjproject/trunk/pjlib/src/pjlib-test/ioq_udp.c
===================================================================
--- /pjproject/trunk/pjlib/src/pjlib-test/ioq_udp.c	(revision 591)
+++ /pjproject/trunk/pjlib/src/pjlib-test/ioq_udp.c	(revision 592)
@@ -41,5 +41,5 @@
 #define THIS_FILE	    "test_udp"
 #define PORT		    51233
-#define LOOP		    100
+#define LOOP		    2
 ///#define LOOP		    2
 #define BUF_MIN_SIZE	    32
@@ -818,5 +818,5 @@
     int bufsize, sock_count;
 
-    goto pass1;
+    //goto pass1;
 
     PJ_LOG(3, (THIS_FILE, "...compliance test (%s)", pj_ioqueue_name()));
@@ -837,4 +837,6 @@
     }
     
+    //return 0;
+
     PJ_LOG(4, (THIS_FILE, "...benchmarking different buffer size:"));
     PJ_LOG(4, (THIS_FILE, "... note: buf=bytes sent, fds=# of fds, "
@@ -848,5 +850,5 @@
     PJ_LOG(3, (THIS_FILE, "...====================================="));
 
-    goto pass2;
+    //goto pass2;
 
     for (bufsize=BUF_MIN_SIZE; bufsize <= BUF_MAX_SIZE; bufsize *= 2) {
@@ -860,5 +862,5 @@
 	 sock_count *= 2) 
     {
-	PJ_LOG(3,(THIS_FILE, "...testing with %d fds", sock_count));
+	//PJ_LOG(3,(THIS_FILE, "...testing with %d fds", sock_count));
 	if ((status=bench_test(bufsize, sock_count-2)) != 0)
 	    return status;
Index: /pjproject/trunk/pjlib/src/pjlib-test/test.h
===================================================================
--- /pjproject/trunk/pjlib/src/pjlib-test/test.h	(revision 591)
+++ /pjproject/trunk/pjlib/src/pjlib-test/test.h	(revision 592)
@@ -25,5 +25,5 @@
 #define GROUP_OS                    0
 #define GROUP_DATA_STRUCTURE        0
-#define GROUP_NETWORK               0
+#define GROUP_NETWORK               1
 #define GROUP_FILE                  0
 
@@ -46,5 +46,5 @@
 #define INCLUDE_SOCK_PERF_TEST      GROUP_NETWORK
 #define INCLUDE_SELECT_TEST	    GROUP_NETWORK
-#define INCLUDE_UDP_IOQUEUE_TEST    1	//GROUP_NETWORK
+#define INCLUDE_UDP_IOQUEUE_TEST    GROUP_NETWORK
 #define INCLUDE_TCP_IOQUEUE_TEST    GROUP_NETWORK
 #define INCLUDE_IOQUEUE_PERF_TEST   GROUP_NETWORK
