+#include <poll.h>
+
+static int io_wait(memcached_st *ptr, unsigned int server_key, unsigned read_or_write)
+{
+ struct pollfd fds[1];
+ short flags= 0;
+ int error;
+ int latch= 0;
+
+ if (read_or_write)
+ flags= POLLOUT | POLLERR;
+ else
+ flags= POLLIN | POLLERR;
+
+ memset(&fds, 0, sizeof(struct pollfd));
+ fds[0].fd= ptr->hosts[server_key].fd;
+ fds[0].events= flags;
+
+ while (latch == 0)
+ {
+ error= poll(fds, 1, ptr->poll_timeout);
+
+ if (error == 1)
+ return MEMCACHED_SUCCESS;
+ else if (error == -1)
+ {
+ memcached_quit_server(ptr, server_key, 1);
+ return MEMCACHED_FAILURE;
+ }
+ else if (error)
+ {
+ /* This is impossible */
+ WATCHPOINT_ASSERT(0);
+ return MEMCACHED_FAILURE;
+ }
+ else
+ latch++;
+ }
+
+ memcached_quit_server(ptr, server_key, 1);
+
+ return MEMCACHED_FAILURE; /* Timeout occurred */
+}