Index: src/xplcommon/Failable.h
===================================================================
--- src/xplcommon/Failable.h	(revision 6)
+++ src/xplcommon/Failable.h	(revision 14)
@@ -82,4 +82,33 @@
 
 //------------------------------------------------------------------------------
+
+/**
+ * An object that references another failable object and fails
+ * together with that object.
+ *
+ * The Child class should contain a function named getFailable()
+ * which returns the failable object.
+ */
+template <class Child>
+class FailableReference
+{
+public:
+    /**
+     * Indicate if the referenced object has failed.
+     */
+    bool failed() const;
+
+    /**
+     * Get the error code from the referenced object.
+     */
+    errorCode_t getErrorCode() const;
+
+    /**
+     * Discard the error code of the referenced object.
+     */
+    void repair();
+};
+
+//------------------------------------------------------------------------------
 // Inline definitions
 //------------------------------------------------------------------------------
@@ -119,4 +148,29 @@
 
 //------------------------------------------------------------------------------
+//------------------------------------------------------------------------------
+
+template <class Child>
+inline bool FailableReference<Child>::failed() const
+{
+    return static_cast<const Child*>(this)->getFailable().failed();
+}
+
+//------------------------------------------------------------------------------
+
+template <class Child>
+inline errorCode_t FailableReference<Child>::getErrorCode() const
+{
+    return static_cast<const Child*>(this)->getFailable().getErrorCode();
+}
+
+//------------------------------------------------------------------------------
+
+template <class Child>
+inline void FailableReference<Child>::repair()
+{
+    static_cast<Child*>(this)->getFailable().repair();
+}
+
+//------------------------------------------------------------------------------
 
 } /* namespace xplcommon */
Index: src/xplcommon/Waiter.h
===================================================================
--- src/xplcommon/Waiter.h	(revision 7)
+++ src/xplcommon/Waiter.h	(revision 14)
@@ -46,5 +46,5 @@
  * instance is passed to them.
  */
-class Waiter
+class Waiter : public Failable
 {
 public:
Index: src/xplcommon/posix/Acceptor.cc
===================================================================
--- src/xplcommon/posix/Acceptor.cc	(revision 6)
+++ src/xplcommon/posix/Acceptor.cc	(revision 14)
@@ -34,6 +34,4 @@
 #include "ServerSocket.h"
 
-#include <cerrno>
-
 #include <poll.h>
 #include <sys/socket.h>
@@ -57,14 +55,12 @@
     if (acceptedFD>=0) return true;
 
-    socklen_t addrlen = addressLength;
-    int result = ::accept(socket.fd,
-                          reinterpret_cast<struct ::sockaddr*>(address),
-                          &addrlen);
+    size_t addrlen = addressLength;
+    int result = socket.accept(reinterpret_cast<struct ::sockaddr*>(address),
+                               (address==0) ? 0 : &addrlen);
     if (result<0) {
-        if (errno==EAGAIN || errno==EWOULDBLOCK) {
+        if (socket.failed()) {
+            socket.events &= ~POLLIN;
+        } else {
             socket.events |= POLLIN;
-        } else {
-            socket.events &= ~POLLIN;
-            setErrorCode(errno);
         }
         return false;
Index: src/xplcommon/posix/Acceptor.h
===================================================================
--- src/xplcommon/posix/Acceptor.h	(revision 11)
+++ src/xplcommon/posix/Acceptor.h	(revision 14)
@@ -34,4 +34,6 @@
 #include "../Failable.h"
 
+#include "ServerSocket.h"
+
 #include <cstdlib>
 
@@ -42,13 +44,9 @@
 //------------------------------------------------------------------------------
 
-class ServerSocket;
-
-//------------------------------------------------------------------------------
-
 /**
  * Class representing the accepting of an incoming connection on a
  * socket.
  */
-class Acceptor : public ::xplcommon::Failable
+class Acceptor : public ::xplcommon::FailableReference<Acceptor>
 {
 protected:
@@ -106,4 +104,17 @@
      */
     void connectionProcessed();
+
+private:
+    /**
+     * Get the failable object.
+     */
+    const ::xplcommon::Failable& getFailable() const;
+
+    /**
+     * Get the failable object.
+     */
+    ::xplcommon::Failable& getFailable();
+
+    friend class FailableReference<Acceptor>;
 };
 
@@ -129,4 +140,18 @@
 //------------------------------------------------------------------------------
 
+inline const ::xplcommon::Failable& Acceptor::getFailable() const
+{
+    return socket;
+}
+
+//------------------------------------------------------------------------------
+
+inline ::xplcommon::Failable& Acceptor::getFailable()
+{
+    return socket;
+}
+
+//------------------------------------------------------------------------------
+
 } /* namespace xplcommon::posix */ } /* namespace xplcommon */
 
Index: src/xplcommon/posix/BufferedWaitable.cc
===================================================================
--- src/xplcommon/posix/BufferedWaitable.cc	(revision 10)
+++ src/xplcommon/posix/BufferedWaitable.cc	(revision 14)
@@ -68,5 +68,9 @@
 ssize_t BufferedWaitable::read(void* dest, size_t length)
 {
-    return ::read(fd, dest, length);
+    ssize_t result = ::read(fd, dest, length);
+    if (result<0 && errno!=EAGAIN && errno!=EWOULDBLOCK) {
+        setErrorCodeFromErrno();
+    }
+    return result;
 }
 
@@ -75,5 +79,9 @@
 ssize_t BufferedWaitable::write(const void* src, size_t length)
 {
-    return ::write(fd, src, length);
+    ssize_t result = ::write(fd, src, length);
+    if (result<0 && errno!=EAGAIN && errno!=EWOULDBLOCK) {
+        setErrorCodeFromErrno();
+    }
+    return result;
 }
 
Index: src/xplcommon/posix/BufferedWaitable.h
===================================================================
--- src/xplcommon/posix/BufferedWaitable.h	(revision 10)
+++ src/xplcommon/posix/BufferedWaitable.h	(revision 14)
@@ -110,5 +110,6 @@
      *
      * @return the number of bytes read (0 in case of end-of-file or
-     * when the connection is closed), -1 on error.
+     * when the connection is closed), -1 on error. In case of error,
+     * the error code will be set up.
      */
     virtual ssize_t read(void* dest, size_t length);
@@ -121,5 +122,6 @@
      * writing.
      *
-     * @return the number of bytes written, or -1 on error.
+     * @return the number of bytes written, or -1 on error. In case of error,
+     * the error code will be set up.
      */
     virtual ssize_t write(const void* src, size_t length);
Index: src/xplcommon/posix/ClientSocket.cc
===================================================================
--- src/xplcommon/posix/ClientSocket.cc	(revision 6)
+++ src/xplcommon/posix/ClientSocket.cc	(revision 14)
@@ -36,4 +36,6 @@
 #include <poll.h>
 
+#include <sys/socket.h>
+
 //------------------------------------------------------------------------------
 
@@ -72,4 +74,15 @@
 //------------------------------------------------------------------------------
 
+bool ClientSocket::connect(const struct sockaddr* addr, size_t addrlen)
+{
+    int result = ::connect(fd, addr, addrlen);
+    if (result<0 && errno!=EINPROGRESS) {
+        setErrorCodeFromErrno();
+    }
+    return result>=0;
+}
+
+//------------------------------------------------------------------------------
+
 // Local Variables:
 // mode: C++
Index: src/xplcommon/posix/ClientSocket.h
===================================================================
--- src/xplcommon/posix/ClientSocket.h	(revision 6)
+++ src/xplcommon/posix/ClientSocket.h	(revision 14)
@@ -37,4 +37,8 @@
 
 namespace xplcommon { namespace posix {
+
+//------------------------------------------------------------------------------
+
+class Connector;
 
 //------------------------------------------------------------------------------
@@ -84,4 +88,14 @@
     virtual void handleEvents(short events);
 
+    /**
+     * Connect the socket to the given address.
+     *
+     * @return whether the connection succeeded or not. If it does not
+     * succeeded, it may be either because of the socket being
+     * non-blocking and some time is needed, or because of a failure,
+     * in which case the error code will be set properly.
+     */
+    bool connect(const struct sockaddr* addr, size_t addrlen);
+
     friend class Connector;
 };
Index: src/xplcommon/posix/Connector.cc
===================================================================
--- src/xplcommon/posix/Connector.cc	(revision 6)
+++ src/xplcommon/posix/Connector.cc	(revision 14)
@@ -34,6 +34,4 @@
 #include "ClientSocket.h"
 
-#include <cerrno>
-
 #include <poll.h>
 #include <sys/socket.h>
@@ -53,17 +51,14 @@
     const struct sockaddr* addr = getAddress(addrlen);
 
-    int result = ::connect(socket.fd, addr, addrlen);
-    if (result<0) {
-        if (errno==EINPROGRESS) {
-            connecting = true;
-            socket.events |= POLLOUT;
-        } else {
-            setErrorCode(errno);
-        }
+    if (socket.connect(addr, addrlen)) {
+        connected = true;
+        return true;
+    } else if (socket.failed()) {
+        return false;
     } else {
-        connected = true;
+        connecting = true;
+        socket.events |= POLLOUT;
+        return false;
     }
-
-    return result==0;
 }
 
@@ -80,10 +75,10 @@
     socklen_t len = sizeof(error);
     if (getsockopt(socket.fd, SOL_SOCKET, SO_ERROR, &error, &len)<0) {
-        setErrorCode(errno);
+        socket.setErrorCodeFromErrno();
     } else {
         if (error==0) {
             connected = true;
         } else {
-            setErrorCode(error);
+            socket.setErrorCode(error);
         }
     }
Index: src/xplcommon/posix/Connector.h
===================================================================
--- src/xplcommon/posix/Connector.h	(revision 6)
+++ src/xplcommon/posix/Connector.h	(revision 14)
@@ -34,4 +34,6 @@
 #include "../Failable.h"
 
+#include "ClientSocket.h"
+
 #include <cstdlib>
 
@@ -46,13 +48,9 @@
 //------------------------------------------------------------------------------
 
-class ClientSocket;
-
-//------------------------------------------------------------------------------
-
 /**
  * Class representing the creation of a connection to some remote
  * entity, like a socket or a pipe.
  */
-class Connector : public Failable
+class Connector : public FailableReference<Connector>
 {
 private:
@@ -112,4 +110,16 @@
     void handleWritable();
 
+private:
+    /**
+     * Get the failable object.
+     */
+    const ::xplcommon::Failable& getFailable() const;
+
+    /**
+     * Get the failable object.
+     */
+    ::xplcommon::Failable& getFailable();
+
+    friend class FailableReference<Connector>;
     friend class ClientSocket;
 };
@@ -128,4 +138,18 @@
 //------------------------------------------------------------------------------
 
+inline const ::xplcommon::Failable& Connector::getFailable() const
+{
+    return socket;
+}
+
+//------------------------------------------------------------------------------
+
+inline ::xplcommon::Failable& Connector::getFailable()
+{
+    return socket;
+}
+
+//------------------------------------------------------------------------------
+
 } /* namespace xplcommon::posix */ } /* namespace xplcommon */
 
Index: src/xplcommon/posix/LocalServerSocket.cc
===================================================================
--- src/xplcommon/posix/LocalServerSocket.cc	(revision 13)
+++ src/xplcommon/posix/LocalServerSocket.cc	(revision 14)
@@ -33,5 +33,4 @@
 
 #include <cstdio>
-#include <cerrno>
 
 #include <pwd.h>
Index: src/xplcommon/posix/LocalServerSocket.h
===================================================================
--- src/xplcommon/posix/LocalServerSocket.h	(revision 13)
+++ src/xplcommon/posix/LocalServerSocket.h	(revision 14)
@@ -33,5 +33,4 @@
 
 #include "ServerSocket.h"
-#include "../Failable.h"
 
 #include "LocalAcceptor.h"
@@ -51,5 +50,5 @@
  * actual socket will be created with the name /tmp/<name>-<username>.
  */
-class LocalServerSocket : public ServerSocket, public ::xplcommon::Failable
+class LocalServerSocket : public ServerSocket
 {
 private:
Index: src/xplcommon/posix/ReadingBuffer.cc
===================================================================
--- src/xplcommon/posix/ReadingBuffer.cc	(revision 6)
+++ src/xplcommon/posix/ReadingBuffer.cc	(revision 14)
@@ -34,6 +34,4 @@
 #include "BufferedWaitable.h"
 
-#include <cerrno>
-
 #include <poll.h>
 
@@ -55,9 +53,8 @@
     ssize_t result = waitable.read(getData(), getCapacity());
     if (result<0) {
-        if (errno==EAGAIN || errno==EWOULDBLOCK) {
+        if (waitable.failed()) {
+            waitable.events &= ~POLLIN;
+        } else {
             waitable.events |= POLLIN;
-        } else {
-            waitable.events &= ~POLLIN;
-            setErrorCode(errno);
         }
     } else {
Index: src/xplcommon/posix/ServerSocket.cc
===================================================================
--- src/xplcommon/posix/ServerSocket.cc	(revision 6)
+++ src/xplcommon/posix/ServerSocket.cc	(revision 14)
@@ -34,6 +34,4 @@
 #include "Socket.h"
 
-#include <cstdio>
-
 #include <unistd.h>
 #include <sys/socket.h>
@@ -49,4 +47,5 @@
     Waitable(waiter, Socket::socket(domain, type, protocol))
 {
+    if (fd<0) setErrorCodeFromErrno();
 }
 
@@ -55,28 +54,52 @@
 ServerSocket::~ServerSocket()
 {
-    ::close(fd);
+    if (fd>=0) ::close(fd);
 }
 
 //------------------------------------------------------------------------------
 
-int ServerSocket::bind(const struct sockaddr* addr, size_t addrlen,
-                       bool reuseaddr)
+bool ServerSocket::bind(const struct sockaddr* addr, size_t addrlen,
+                        bool reuseaddr)
 {
     if (reuseaddr) {
         int reuse = 1;
         if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse))<0) {
-            perror("xplcommon::posix::Socket::bind: setsockopt");
-            return -1;
+            setErrorCodeFromErrno();
+            return false;
         }
     }
 
-    return ::bind(fd, addr, addrlen);
+    if (::bind(fd, addr, addrlen)<0) {
+        setErrorCodeFromErrno();
+        return false;
+    }
+
+    return true;
 }
 
 //------------------------------------------------------------------------------
 
-int ServerSocket::listen(int backlog)
+bool ServerSocket::listen(int backlog)
 {
-    return ::listen(fd, backlog);
+    if (::listen(fd, backlog)<0) {
+        setErrorCodeFromErrno();
+        return false;
+    } else {
+        return true;
+    }
+}
+
+//------------------------------------------------------------------------------
+
+int ServerSocket::accept(struct sockaddr* addr, size_t* addrlen)
+{
+    socklen_t alen = (addrlen==0) ? 0 : *addrlen;
+    int result = ::accept(fd, addr, (addrlen==0) ? 0 : &alen);
+    if (result<0 && errno!=EAGAIN && errno!=EWOULDBLOCK) {
+        setErrorCodeFromErrno();
+    } else {
+        if (addrlen!=0) *addrlen = alen;
+    }
+    return result;
 }
 
Index: src/xplcommon/posix/ServerSocket.h
===================================================================
--- src/xplcommon/posix/ServerSocket.h	(revision 6)
+++ src/xplcommon/posix/ServerSocket.h	(revision 14)
@@ -75,12 +75,27 @@
      * @param reuseaddr if set to true, the SO_REUSEADDR socket option
      * will be set.
+     *
+     * @return if the binding has succeeded. If it is false, the error
+     * code will be set accordingly.
      */
-    int bind(const struct sockaddr* addr, size_t addrlen,
-             bool reuseaddr = true);
+    bool bind(const struct sockaddr* addr, size_t addrlen,
+              bool reuseaddr = true);
 
     /**
      * Start listening on the socket.
+     *
+     * @return if the call has succeeded. If it is false, the error
+     * code will be set accordingly.
      */
-    int listen(int backlog = 5);
+    bool listen(int backlog = 5);
+
+    /**
+     * Accept a socket.
+     *
+     * @return the accepted socket. If may be -1, if the socket is
+     * non-blocking, and no connection is available yet, or if another
+     * error occured, in which case the error code will be set properly.
+     */
+    int accept(struct sockaddr* addr, size_t* addrlen);
 
     friend class Acceptor;
Index: src/xplcommon/posix/Socket.cc
===================================================================
--- src/xplcommon/posix/Socket.cc	(revision 6)
+++ src/xplcommon/posix/Socket.cc	(revision 14)
@@ -32,6 +32,4 @@
 #include "Socket.h"
 
-#include <cstdio>
-
 #include <unistd.h>
 #include <sys/socket.h>
@@ -45,9 +43,5 @@
 int Socket::socket(int domain, int type, int protocol)
 {
-    int fd = ::socket(domain, type, protocol);
-    if (fd<0) {
-        perror("xplcommon::posix::Socket::socket: socket");
-    }
-    return fd;
+    return ::socket(domain, type, protocol);
 }
 
@@ -56,5 +50,5 @@
 Socket::~Socket()
 {
-    ::close(fd);
+    if (fd>=0) ::close(fd);
 }
 
Index: src/xplcommon/posix/Socket.h
===================================================================
--- src/xplcommon/posix/Socket.h	(revision 11)
+++ src/xplcommon/posix/Socket.h	(revision 14)
@@ -41,8 +41,4 @@
 
 namespace xplcommon { namespace posix {
-
-//------------------------------------------------------------------------------
-
-class Connector;
 
 //------------------------------------------------------------------------------
@@ -92,4 +88,5 @@
                      readingCapacity, writingCapacity)
 {
+    if (fd<0) setErrorCodeFromErrno();
 }
 
Index: src/xplcommon/posix/Waitable.h
===================================================================
--- src/xplcommon/posix/Waitable.h	(revision 11)
+++ src/xplcommon/posix/Waitable.h	(revision 14)
@@ -32,4 +32,10 @@
 //------------------------------------------------------------------------------
 
+#include "../Failable.h"
+
+#include <cerrno>
+
+//------------------------------------------------------------------------------
+
 namespace xplcommon { namespace posix {
 
@@ -45,5 +51,5 @@
  * It is basically a file descriptor.
  */
-class Waitable
+class Waitable : public Failable
 {
 public:
@@ -118,4 +124,9 @@
     virtual void handleEvents(short events) = 0;
 
+    /**
+     * Set the error code from errno.
+     */
+    void setErrorCodeFromErrno();
+
     friend class Waiter;
 };
@@ -146,4 +157,11 @@
 //------------------------------------------------------------------------------
 
+inline void Waitable::setErrorCodeFromErrno()
+{
+    setErrorCode(errno);
+}
+
+//------------------------------------------------------------------------------
+
 } /* namespace xplcommon::posix */ } /* namespace xplcommon */
 
Index: src/xplcommon/posix/WaitableBuffer.h
===================================================================
--- src/xplcommon/posix/WaitableBuffer.h	(revision 6)
+++ src/xplcommon/posix/WaitableBuffer.h	(revision 14)
@@ -35,11 +35,9 @@
 #include "../Failable.h"
 
+#include "BufferedWaitable.h"
+
 //------------------------------------------------------------------------------
 
 namespace xplcommon { namespace posix {
-
-//------------------------------------------------------------------------------
-
-class BufferedWaitable;
 
 //------------------------------------------------------------------------------
@@ -49,5 +47,5 @@
  */
 class WaitableBuffer : public ::xplcommon::Buffer,
-                       public ::xplcommon::Failable
+                       public ::xplcommon::FailableReference<WaitableBuffer>
 {
 protected:
@@ -62,4 +60,17 @@
      */
     WaitableBuffer(size_t capacity, BufferedWaitable* waitable);
+
+private:
+    /**
+     * Get the failable object.
+     */
+    const ::xplcommon::Failable& getFailable() const;
+
+    /**
+     * Get the failable object.
+     */
+    ::xplcommon::Failable& getFailable();
+
+    friend class FailableReference<WaitableBuffer>;
 };
 
@@ -77,4 +88,18 @@
 //------------------------------------------------------------------------------
 
+inline const ::xplcommon::Failable& WaitableBuffer::getFailable() const
+{
+    return waitable;
+}
+
+//------------------------------------------------------------------------------
+
+inline ::xplcommon::Failable& WaitableBuffer::getFailable()
+{
+    return waitable;
+}
+
+//------------------------------------------------------------------------------
+
 } /* namespace xplcommon::posix */ } /* namespace xplcommon */
 
Index: src/xplcommon/posix/WaitableEvent.cc
===================================================================
--- src/xplcommon/posix/WaitableEvent.cc	(revision 8)
+++ src/xplcommon/posix/WaitableEvent.cc	(revision 14)
@@ -32,8 +32,4 @@
 #include "WaitableEvent.h"
 
-#include <cassert>
-#include <cerrno>
-#include <cstdio>
-
 #include <poll.h>
 #include <unistd.h>
@@ -51,5 +47,7 @@
     fired(false)
 {
-    assert(fd>=0);
+    if (fd<0) {
+        setErrorCodeFromErrno();
+    }
 }
 
@@ -68,5 +66,5 @@
     if (write(fd, &one, sizeof(one))<0) {
         if (errno!=EAGAIN) {
-            setErrorCode(errno);
+            setErrorCodeFromErrno();
         }
     }
@@ -111,5 +109,5 @@
     if (read(fd, &value, sizeof(value))<0) {
         if (errno!=EAGAIN) {
-            setErrorCode(errno);
+            setErrorCodeFromErrno();
         }
     } else {
Index: src/xplcommon/posix/WaitableEvent.h
===================================================================
--- src/xplcommon/posix/WaitableEvent.h	(revision 8)
+++ src/xplcommon/posix/WaitableEvent.h	(revision 14)
@@ -45,5 +45,5 @@
  * An event on which one can wait with a waiter.
  */
-class WaitableEvent : public Waitable, public Failable
+class WaitableEvent : public Waitable
 {
 private:
Index: src/xplcommon/posix/Waiter.cc
===================================================================
--- src/xplcommon/posix/Waiter.cc	(revision 3)
+++ src/xplcommon/posix/Waiter.cc	(revision 14)
@@ -35,5 +35,5 @@
 
 #include <cassert>
-#include <cstdio>
+#include <cerrno>
 
 //------------------------------------------------------------------------------
@@ -114,5 +114,5 @@
 
     if (result<0) {
-        perror("xplcommon::posix::Waiter::wait: poll");
+        setErrorCode(errno);
         return false;
     } else if (result>0) {
Index: src/xplcommon/posix/Waiter.h
===================================================================
--- src/xplcommon/posix/Waiter.h	(revision 3)
+++ src/xplcommon/posix/Waiter.h	(revision 14)
@@ -32,4 +32,6 @@
 //------------------------------------------------------------------------------
 
+#include "../Failable.h"
+
 #include <map>
 
@@ -52,5 +54,5 @@
  * for them.
  */
-class Waiter
+class Waiter : public Failable
 {
 private:
Index: src/xplcommon/posix/WritingBuffer.cc
===================================================================
--- src/xplcommon/posix/WritingBuffer.cc	(revision 6)
+++ src/xplcommon/posix/WritingBuffer.cc	(revision 14)
@@ -34,6 +34,4 @@
 #include "BufferedWaitable.h"
 
-#include <cerrno>
-
 #include <poll.h>
 
@@ -66,10 +64,9 @@
     ssize_t result = waitable.write(getData() + offset, toWrite);
     if (result<static_cast<ssize_t>(toWrite)) {
-        if (result>=0 || errno==EAGAIN || errno==EWOULDBLOCK) {
+        if (result<0 && waitable.failed()) {
+            waitable.events &= ~POLLOUT;
+        } else {
             waitable.events |= POLLOUT;
             if (result>0) offset += result;
-        } else {
-            waitable.events &= ~POLLOUT;
-            setErrorCode(errno);
         }
         return false;
