Index: src/xplcommon/win32/BufferedStream.cc
===================================================================
--- src/xplcommon/win32/BufferedStream.cc	(revision 21)
+++ src/xplcommon/win32/BufferedStream.cc	(revision 22)
@@ -64,11 +64,12 @@
 {
     DWORD numRead = 0;
-    overlapped->reset();
-    bool result = ReadFile(handle, dest, length, 0, overlapped->get());
-                           // (overlapped==0) ? &numRead : 0,
-                           // (overlapped==0) ? 0 : overlapped->get());
+    if (overlapped!=0) overlapped->reset();
+    bool result = ReadFile(handle, dest, length,
+                           (overlapped==0) ? &numRead : 0,
+                           (overlapped==0) ? 0 : overlapped->get());
     DWORD error = GetLastError();
-    if (!result && (overlapped==0 ||
-                    (error!=ERROR_IO_PENDING && error!=ERROR_IO_INCOMPLETE)))
+    if (!result &&
+        (overlapped==0 ||
+         (error!=ERROR_IO_PENDING && error!=ERROR_IO_INCOMPLETE)))
     {
         setErrorCode(error);
@@ -86,9 +87,11 @@
                            Overlapped* overlapped)
 {
+    if (overlapped!=0) overlapped->reset();
     bool result = WriteFile(handle, src, length, 0,
                             (overlapped==0) ? 0 : overlapped->get());
     DWORD error = GetLastError();
-    if (!result && (overlapped==0 ||
-                    (error!=ERROR_IO_PENDING && error!=ERROR_IO_INCOMPLETE)))
+    if (!result &&
+        (overlapped==0 ||
+         (error!=ERROR_IO_PENDING && error!=ERROR_IO_INCOMPLETE)))
     {
         setErrorCode(error);
Index: src/xplcommon/win32/Completer.h
===================================================================
--- src/xplcommon/win32/Completer.h	(revision 21)
+++ src/xplcommon/win32/Completer.h	(revision 22)
@@ -51,4 +51,9 @@
 protected:
     /**
+     * The overlappable object this completer belongs to.
+     */
+    Overlappable& overlappable;
+
+    /**
      * The overlapped object.
      */
@@ -64,5 +69,5 @@
      * Destroy the completer.
      */
-    ~Completer();
+    virtual ~Completer();
 
     /**
@@ -88,5 +93,21 @@
      * @see Overlapped::getResult
      */
-    bool getResult(DWORD& size, HANDLE file);
+    bool getResult(DWORD& size);
+
+    /**
+     * Check whether we are waiting for an overlapped result and if
+     * so, return that.
+     *
+     * @param result will contain the result to return if we are
+     * waiting for an overlapped result.
+     *
+     * @return whether we are waiting for an overlapped result
+     */
+    bool checkWaitedResult(bool& result);
+
+    /**
+     * Handle the given result coming from an overlapped object.
+     */
+    virtual void handleWaitedResult(DWORD size) = 0;
 };
 
@@ -96,4 +117,5 @@
 
 inline Completer::Completer(Overlappable* overlappable) :
+    overlappable(*overlappable),
     overlapped((overlappable->waiter==0) ? 0 : new Overlapped(*overlappable))
 {
@@ -130,7 +152,28 @@
 //------------------------------------------------------------------------------
 
-inline bool Completer::getResult(DWORD& size, HANDLE file)
+inline bool Completer::getResult(DWORD& size)
 {
-    return overlapped!=0 && overlapped->getResult(size, file);
+    return overlapped!=0 && overlapped->getResult(size, overlappable.handle);
+}
+
+//------------------------------------------------------------------------------
+
+inline bool Completer::checkWaitedResult(bool& result)
+{
+    if (!isWaited()) {
+        return false;
+    }
+
+    DWORD size = 0;
+    result = getResult(size);
+
+    if (result) {
+        removeFromWaiter();
+        handleWaitedResult(size);
+    } else if (overlappable.failed()) {
+        removeFromWaiter();
+    }
+
+    return true;
 }
 
Index: src/xplcommon/win32/LocalAcceptor.cc
===================================================================
--- src/xplcommon/win32/LocalAcceptor.cc	(revision 21)
+++ src/xplcommon/win32/LocalAcceptor.cc	(revision 22)
@@ -43,5 +43,5 @@
     accepted(false)
 {
-    LocalSocket::setupName(this->name, name);
+    LocalSocket::setupPath(path, name);
 }
 
@@ -50,26 +50,17 @@
 bool LocalAcceptor::accept()
 {
+    if (failed()) return false;
     if (accepted) return true;
 
-    if (isWaited()) {
-        DWORD size = 0;
-        if (getResult(size, socket.handle)) {
-            removeFromWaiter();
-            accepted = true;
-            return true;
-        } else {
-            if (socket.failed()) {
-                removeFromWaiter();
-            }
-            return false;
-        }
+    bool result = false;
+    if (checkWaitedResult(result)) {
+        return result;
     }
 
-
-    if (!socket.createNamedPipe(name)) {
+    if (!socket.createNamedPipe(path)) {
         return false;
     }
 
-    bool result = socket.connectNamedPipe(overlapped);
+    result = socket.connectNamedPipe(overlapped);
     if (result) {
         accepted = true;
@@ -83,4 +74,11 @@
 //------------------------------------------------------------------------------
 
+void LocalAcceptor::handleWaitedResult(DWORD /*size*/)
+{
+    accepted = true;
+}
+
+//------------------------------------------------------------------------------
+
 // Local Variables:
 // mode: C++
Index: src/xplcommon/win32/LocalAcceptor.h
===================================================================
--- src/xplcommon/win32/LocalAcceptor.h	(revision 21)
+++ src/xplcommon/win32/LocalAcceptor.h	(revision 22)
@@ -57,7 +57,7 @@
 
     /**
-     * The name of the pipe to accept.
+     * The path of the pipe to accept.
      */
-    char name[256];
+    char path[256];
 
     /**
@@ -103,4 +103,12 @@
 
 
+protected:
+    /**
+     * Set the indicator of an accepted connection to true.
+     *
+     * @see Completer::handleWaitedResult
+     */
+    virtual void handleWaitedResult(DWORD size);
+
 private:
     /**
@@ -128,6 +136,6 @@
     if (!accepted) return 0;
 
-    LocalSocket* s= new LocalSocket(waiter, socket.releaseHandle(),
-                                    readingCapacity, writingCapacity);
+    LocalSocket* s = new LocalSocket(waiter, socket.releaseHandle(),
+                                     readingCapacity, writingCapacity);
     accepted = false;
     return s;
Index: src/xplcommon/win32/LocalClientSocket.cc
===================================================================
--- src/xplcommon/win32/LocalClientSocket.cc	(revision 21)
+++ src/xplcommon/win32/LocalClientSocket.cc	(revision 22)
@@ -38,16 +38,4 @@
 //------------------------------------------------------------------------------
 
-HANDLE LocalClientSocket::create(const char* name, bool overlapped)
-{
-    char buf[256];
-    setupName(buf, name);
-
-    return CreateFile(buf, GENERIC_READ|GENERIC_WRITE,
-                      0, 0, OPEN_EXISTING,
-                      FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED, 0);
-}
-
-//------------------------------------------------------------------------------
-
 // Local Variables:
 // mode: C++
Index: src/xplcommon/win32/LocalClientSocket.h
===================================================================
--- src/xplcommon/win32/LocalClientSocket.h	(revision 21)
+++ src/xplcommon/win32/LocalClientSocket.h	(revision 22)
@@ -51,9 +51,4 @@
 private:
     /**
-     * Create a named pipe for the given user-specified name.
-     */
-    static HANDLE create(const char* name, bool overlapped);
-
-    /**
      * The local connector.
      */
Index: src/xplcommon/win32/LocalConnector.cc
===================================================================
--- src/xplcommon/win32/LocalConnector.cc	(revision 21)
+++ src/xplcommon/win32/LocalConnector.cc	(revision 22)
@@ -42,21 +42,11 @@
 bool LocalConnector::connect()
 {
-    printf("LocalConnector::connect0\n");
     if (connected) return true;
+    if (failed()) return false;
 
-    printf("LocalConnector::connect3\n");
-
-    HANDLE handle = CreateFile(name, GENERIC_READ|GENERIC_WRITE,
-                               0, 0, OPEN_EXISTING,
-                               FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED, 0);
-    printf("LocalConnector::connect4\n");
-    if (handle!=0) {
-        printf("LocalConnector::connect5\n");
-        socket.handle = handle;
+    if (socket.connect(path)) {
         connected = true;
         return true;
     } else {
-        printf("LocalConnector::connect6\n");
-        socket.setErrorCode(GetLastError());
         return false;
     }
Index: src/xplcommon/win32/LocalConnector.h
===================================================================
--- src/xplcommon/win32/LocalConnector.h	(revision 21)
+++ src/xplcommon/win32/LocalConnector.h	(revision 22)
@@ -52,7 +52,7 @@
 
     /**
-     * The name of the pipe to connect to.
+     * The path of the pipe to connect to.
      */
-    char name[256];
+    char path[256];
 
     /**
@@ -105,5 +105,5 @@
     connected(false)
 {
-    LocalSocket::setupName(this->name, name);
+    LocalSocket::setupPath(path, name);
 }
 
Index: src/xplcommon/win32/LocalServerSocketBase.cc
===================================================================
--- src/xplcommon/win32/LocalServerSocketBase.cc	(revision 21)
+++ src/xplcommon/win32/LocalServerSocketBase.cc	(revision 22)
@@ -40,7 +40,7 @@
 //------------------------------------------------------------------------------
 
-bool LocalServerSocketBase::createNamedPipe(const char* name)
+bool LocalServerSocketBase::createNamedPipe(const char* path)
 {
-    handle = CreateNamedPipe(name, PIPE_ACCESS_DUPLEX |
+    handle = CreateNamedPipe(path, PIPE_ACCESS_DUPLEX |
                              ((waiter==0) ? 0 : FILE_FLAG_OVERLAPPED),
                              PIPE_TYPE_BYTE | PIPE_READMODE_BYTE,
@@ -59,4 +59,5 @@
 bool LocalServerSocketBase::connectNamedPipe(Overlapped* overlapped)
 {
+    if (overlapped!=0) overlapped->reset();
     bool result = LocalSocket::connectNamedPipe(handle, overlapped);
     if (!result && overlapped==0) {
Index: src/xplcommon/win32/LocalServerSocketBase.h
===================================================================
--- src/xplcommon/win32/LocalServerSocketBase.h	(revision 21)
+++ src/xplcommon/win32/LocalServerSocketBase.h	(revision 22)
@@ -60,10 +60,10 @@
      * this object will be set to the created handle.
      *
-     * @param name the name of the pipe to create. It should be the
-     * final name, not the user-given one.
+     * @param path the path of the pipe to create. It should be the
+     * final path, not the user-given name.
      *
      * @return whether the pipe could be created
      */
-    bool createNamedPipe(const char* name);
+    bool createNamedPipe(const char* path);
 
     /**
Index: src/xplcommon/win32/LocalSocket.h
===================================================================
--- src/xplcommon/win32/LocalSocket.h	(revision 21)
+++ src/xplcommon/win32/LocalSocket.h	(revision 22)
@@ -51,10 +51,10 @@
 public:
     /**
-     * Create a concrete pipe name from the given user-specified name.
+     * Create a concrete pipe path from the given user-specified name.
      *
      * @param dest the destination buffer, which should be at least
      * 256 bytes long.
      */
-    static void setupName(char* dest, const char* name);
+    static void setupPath(char* dest, const char* name);
 
     /**
@@ -90,11 +90,7 @@
 protected:
     /**
-     * Connect the named pipe with the current handle of this object.
-     *
-     * @return whether the connection succeeded. If not, it may be a
-     * failure, which is then indicated, or it may be an overlapped
-     * operation which needs to wait.
+     * Connect the client socket to the server.
      */
-    bool connect(Overlapped* overlapped);
+    bool connect(const char* path);
 
     friend class LocalAcceptor;
@@ -106,5 +102,5 @@
 //------------------------------------------------------------------------------
 
-inline void LocalSocket::setupName(char* dest, const char* name)
+inline void LocalSocket::setupPath(char* dest, const char* name)
 {
     snprintf(dest, 256, "\\\\.\\pipe\\%s", name);
@@ -134,11 +130,14 @@
 //------------------------------------------------------------------------------
 
-inline bool LocalSocket::connect(Overlapped* overlapped)
+inline bool LocalSocket::connect(const char* path)
 {
-    bool result = connectNamedPipe(handle, overlapped);
-    if (!result && overlapped==0) {
+    handle = CreateFile(path, GENERIC_READ|GENERIC_WRITE,
+                        0, 0, OPEN_EXISTING,
+                        FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED, 0);
+    if (handle==0) {
         setErrorCode(GetLastError());
     }
-    return result;
+
+    return handle!=0;
 }
 
Index: src/xplcommon/win32/Overlapped.cc
===================================================================
--- src/xplcommon/win32/Overlapped.cc	(revision 21)
+++ src/xplcommon/win32/Overlapped.cc	(revision 22)
@@ -42,4 +42,5 @@
     memset(&overlapped, 0, sizeof(overlapped));
     overlapped.hEvent = getHandle();
+    clear();
 }
 
@@ -52,9 +53,6 @@
         DWORD error = GetLastError();
         if (error!=ERROR_IO_PENDING && error!=ERROR_IO_INCOMPLETE) {
-            reset();
             setErrorCode(error);
         }
-    } else {
-        reset();
     }
     return result;
Index: src/xplcommon/win32/Overlapped.h
===================================================================
--- src/xplcommon/win32/Overlapped.h	(revision 21)
+++ src/xplcommon/win32/Overlapped.h	(revision 22)
@@ -62,5 +62,6 @@
 
     /**
-     * Reset the overlapped structure and set its event handle.
+     * Reset the overlapped structure and set its event handle. The
+     * event is also cleared.
      */
     void reset();
@@ -100,5 +101,4 @@
     Event(eventFailable)
 {
-    reset();
 }
 
Index: src/xplcommon/win32/ReadingBuffer.cc
===================================================================
--- src/xplcommon/win32/ReadingBuffer.cc	(revision 21)
+++ src/xplcommon/win32/ReadingBuffer.cc	(revision 22)
@@ -48,32 +48,25 @@
     }
 
-    if (isWaited()) {
-        DWORD size = 0;
-        if (getResult(size, stream.handle)) {
-            removeFromWaiter();
-            overlapped->clear();
-            addLength(size);
-            return true;
-        } else {
-            if (stream.failed()) {
-                overlapped->clear();
-                removeFromWaiter();
-            }
-            return false;
-        }
+    bool result = false;
+    if (checkWaitedResult(result)) {
+        return result;
     }
 
-    ssize_t result = stream.read(getData(), getCapacity(), overlapped);
-    if (result<0) {
+    ssize_t numRead = stream.read(getData(), getCapacity(), overlapped);
+    if (numRead<0) {
         if (!stream.failed()) {
             addTo(stream.getWaiter());
-        } else {
-            overlapped->reset();
         }
     } else {
-        overlapped->reset();
-        addLength(result);
+        addLength(numRead);
     }
-    return result>=0;
+    return numRead>=0;
+}
+
+//------------------------------------------------------------------------------
+
+void ReadingBuffer::handleWaitedResult(DWORD size)
+{
+    addLength(size);
 }
 
Index: src/xplcommon/win32/ReadingBuffer.h
===================================================================
--- src/xplcommon/win32/ReadingBuffer.h	(revision 21)
+++ src/xplcommon/win32/ReadingBuffer.h	(revision 22)
@@ -77,4 +77,12 @@
     bool read();
 
+protected:
+    /**
+     * Set the buffer's length to the given value.
+     *
+     * @see Completer::handleWaitedResult
+     */
+    virtual void handleWaitedResult(DWORD size);
+
     friend class BufferedStream;
 };
Index: src/xplcommon/win32/WritingBuffer.cc
===================================================================
--- src/xplcommon/win32/WritingBuffer.cc	(revision 21)
+++ src/xplcommon/win32/WritingBuffer.cc	(revision 22)
@@ -46,21 +46,10 @@
     }
 
-    if (isWaited()) {
-        DWORD size = 0;
-        if (getResult(size, stream.handle)) {
-            removeFromWaiter();
-            overlapped->clear();
-            reset();
-            return true;
-        } else {
-            if (stream.failed()) {
-                removeFromWaiter();
-                overlapped->clear();
-            }
-            return false;
-        }
+    bool result;
+    if (checkWaitedResult(result)) {
+        return result;
     }
 
-    bool result = stream.write(getData(), getLength(), overlapped);
+    result = stream.write(getData(), getLength(), overlapped);
     if (result) {
         reset();
@@ -74,4 +63,11 @@
 //------------------------------------------------------------------------------
 
+void WritingBuffer::handleWaitedResult(DWORD /*size*/)
+{
+    reset();
+}
+
+//------------------------------------------------------------------------------
+
 // Local Variables:
 // mode: C++
Index: src/xplcommon/win32/WritingBuffer.h
===================================================================
--- src/xplcommon/win32/WritingBuffer.h	(revision 21)
+++ src/xplcommon/win32/WritingBuffer.h	(revision 22)
@@ -77,4 +77,12 @@
     bool write();
 
+protected:
+    /**
+     * Reset the buffer to signify that its contents have been written.
+     *
+     * @see Completer::handleWaitedResult
+     */
+    virtual void handleWaitedResult(DWORD size);
+
     friend class BufferedStream;
 };
