Fix timeout handling in x0vncserver

We need to proper respect the timeouts set up by the core RFB code
and not just the polling scheduler.
diff --git a/unix/x0vncserver/x0vncserver.cxx b/unix/x0vncserver/x0vncserver.cxx
index 791714e..caf7814 100644
--- a/unix/x0vncserver/x0vncserver.cxx
+++ b/unix/x0vncserver/x0vncserver.cxx
@@ -508,6 +508,7 @@
     PollingScheduler sched((int)pollingCycle, (int)maxProcessorUsage);
 
     while (!caughtSignal) {
+      int wait_ms;
       struct timeval tv;
       fd_set rfds;
       std::list<Socket*> sockets;
@@ -538,23 +539,24 @@
       if (!clients_connected)
         sched.reset();
 
+      wait_ms = 0;
+
       if (sched.isRunning()) {
-        int wait_ms = sched.millisRemaining();
+        wait_ms = sched.millisRemaining();
         if (wait_ms > 500) {
           wait_ms = 500;
         }
-        tv.tv_usec = wait_ms * 1000;
-#ifdef DEBUG
-        // fprintf(stderr, "[%d]\t", wait_ms);
-#endif
-      } else {
-        tv.tv_usec = 100000;
       }
-      tv.tv_sec = 0;
+
+      soonestTimeout(&wait_ms, server.checkTimeouts());
+
+      tv.tv_sec = wait_ms / 1000;
+      tv.tv_usec = (wait_ms % 1000) * 1000;
 
       // Do the wait...
       sched.sleepStarted();
-      int n = select(FD_SETSIZE, &rfds, 0, 0, &tv);
+      int n = select(FD_SETSIZE, &rfds, 0, 0,
+                     wait_ms ? &tv : NULL);
       sched.sleepFinished();
 
       if (n < 0) {
@@ -580,7 +582,6 @@
         }
       }
 
-      Timer::checkTimeouts();
       server.checkTimeouts();
 
       // Client list could have been changed.