Index: src/xplcommon/Failable.h
===================================================================
--- src/xplcommon/Failable.h	(revision 6)
+++ src/xplcommon/Failable.h	(revision 6)
@@ -0,0 +1,131 @@
+// Copyright (c) 2012 by István Váradi
+
+// This file is part of libxplcommon, a common utility library for
+// development related to X-Plane
+
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+
+// 1. Redistributions of source code must retain the above copyright notice, this
+//    list of conditions and the following disclaimer.
+// 2. Redistributions in binary form must reproduce the above copyright notice,
+//    this list of conditions and the following disclaimer in the documentation
+//    and/or other materials provided with the distribution.
+
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// The views and conclusions contained in the software and documentation are those
+// of the authors and should not be interpreted as representing official policies,
+// either expressed or implied, of the FreeBSD Project.
+
+#ifndef XPLCOMMON_FAILABLE_H
+#define XPLCOMMON_FAILABLE_H
+//------------------------------------------------------------------------------
+
+#include "types.h"
+
+//------------------------------------------------------------------------------
+
+namespace xplcommon {
+
+//------------------------------------------------------------------------------
+
+/**
+ * An object whose instances can fail, and the reason of the failure
+ * is indicated by an integral error code.
+ */
+class Failable
+{
+private:
+    /**
+     * The error code.
+     */
+    errorCode_t errorCode;
+
+public:
+    /**
+     * Construct the object and initialize the error code to indicate
+     * no error.
+     */
+    Failable();
+
+    /**
+     * Indicate if the object has failed.
+     */
+    bool failed() const;
+
+    /**
+     * Get the error code.
+     */
+    errorCode_t getErrorCode() const;
+
+    /**
+     * Discard the error code.
+     */
+    void repair();
+
+protected:
+    /**
+     * Set the error code.
+     */
+    void setErrorCode(errorCode_t ec);
+};
+
+//------------------------------------------------------------------------------
+// Inline definitions
+//------------------------------------------------------------------------------
+
+inline Failable::Failable() :
+    errorCode(NO_ERROR)
+{
+}
+
+//------------------------------------------------------------------------------
+
+inline bool Failable::failed() const
+{
+    return errorCode!=NO_ERROR;
+}
+
+//------------------------------------------------------------------------------
+
+inline errorCode_t Failable::getErrorCode() const
+{
+    return errorCode;
+}
+
+//------------------------------------------------------------------------------
+
+inline void Failable::repair()
+{
+    errorCode = NO_ERROR;
+}
+
+//------------------------------------------------------------------------------
+
+inline void Failable::setErrorCode(errorCode_t ec)
+{
+    errorCode = ec;
+}
+
+//------------------------------------------------------------------------------
+
+} /* namespace xplcommon */
+
+//------------------------------------------------------------------------------
+#endif // XPLCOMMON_FAILABLE_H
+
+// Local Variables:
+// mode: C++
+// c-basic-offset: 4
+// indent-tabs-mode: nil
+// End:
Index: src/xplcommon/Makefile.am
===================================================================
--- src/xplcommon/Makefile.am	(revision 5)
+++ src/xplcommon/Makefile.am	(revision 6)
@@ -23,4 +23,6 @@
 include_xplcommon_HEADERS=\
 	config.h	\
+	types.h		\
+	Failable.h	\
 	Thread.h	\
 	Waiter.h	\
Index: src/xplcommon/posix/Acceptor.cc
===================================================================
--- src/xplcommon/posix/Acceptor.cc	(revision 6)
+++ src/xplcommon/posix/Acceptor.cc	(revision 6)
@@ -0,0 +1,84 @@
+// Copyright (c) 2012 by István Váradi
+
+// This file is part of libxplcommon, a common utility library for
+// development related to X-Plane
+
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+
+// 1. Redistributions of source code must retain the above copyright notice, this
+//    list of conditions and the following disclaimer.
+// 2. Redistributions in binary form must reproduce the above copyright notice,
+//    this list of conditions and the following disclaimer in the documentation
+//    and/or other materials provided with the distribution.
+
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// The views and conclusions contained in the software and documentation are those
+// of the authors and should not be interpreted as representing official policies,
+// either expressed or implied, of the FreeBSD Project.
+
+//------------------------------------------------------------------------------
+
+#include "Acceptor.h"
+
+#include "ServerSocket.h"
+
+#include <cerrno>
+
+#include <poll.h>
+#include <sys/socket.h>
+
+//------------------------------------------------------------------------------
+
+using xplcommon::posix::Acceptor;
+
+//------------------------------------------------------------------------------
+
+Acceptor::~Acceptor()
+{
+    delete [] address;
+}
+
+//------------------------------------------------------------------------------
+
+bool Acceptor::accept()
+{
+    if (failed()) return false;
+    if (acceptedFD>=0) return true;
+
+    socklen_t addrlen = addressLength;
+    int result = ::accept(socket.fd,
+                          reinterpret_cast<struct ::sockaddr*>(address),
+                          &addrlen);
+    if (result<0) {
+        if (errno==EAGAIN || errno==EWOULDBLOCK) {
+            socket.events |= POLLIN;
+        } else {
+            socket.events &= ~POLLIN;
+            setErrorCode(errno);
+        }
+        return false;
+    } else {
+        acceptedFD = result;
+        socket.events &= ~POLLIN;
+        return true;
+    }
+}
+
+//------------------------------------------------------------------------------
+
+// Local Variables:
+// mode: C++
+// c-basic-offset: 4
+// indent-tabs-mode: nil
+// End:
Index: src/xplcommon/posix/Acceptor.h
===================================================================
--- src/xplcommon/posix/Acceptor.h	(revision 6)
+++ src/xplcommon/posix/Acceptor.h	(revision 6)
@@ -0,0 +1,141 @@
+// Copyright (c) 2012 by István Váradi
+
+// This file is part of libxplcommon, a common utility library for
+// development related to X-Plane
+
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+
+// 1. Redistributions of source code must retain the above copyright notice, this
+//    list of conditions and the following disclaimer.
+// 2. Redistributions in binary form must reproduce the above copyright notice,
+//    this list of conditions and the following disclaimer in the documentation
+//    and/or other materials provided with the distribution.
+
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// The views and conclusions contained in the software and documentation are those
+// of the authors and should not be interpreted as representing official policies,
+// either expressed or implied, of the FreeBSD Project.
+
+#ifndef XPLCOMMON_POSIX_ACCEPTOR_H
+#define XPLCOMMON_POSIX_ACCEPTOR_H
+//------------------------------------------------------------------------------
+
+#include "../Failable.h"
+
+#include <cstdlib>
+
+//------------------------------------------------------------------------------
+
+namespace xplcommon { namespace posix {
+
+//------------------------------------------------------------------------------
+
+class ServerSocket;
+
+//------------------------------------------------------------------------------
+
+/**
+ * Class representing the accepting of an incoming connection on a
+ * socket.
+ */
+class Acceptor : public ::xplcommon::Failable
+{
+private:
+    /**
+     * The socket we belong to.
+     */
+    ServerSocket& socket;
+
+protected:
+    /**
+     * The file descriptor accepted the last time.
+     */
+    int acceptedFD;
+
+    /**
+     * The buffer for the remote address, if we are interested.
+     */
+    unsigned char* address;
+
+    /**
+     * The length of the remote address buffer.
+     */
+    size_t addressLength;
+
+protected:
+    /**
+     * Construct the acceptor.
+     *
+     * @param addressLength the length of the buffer for the peer
+     * address. If 0, not buffer will be created.
+     */
+    Acceptor(ServerSocket* socket, size_t addressLength = 0);
+
+public:
+    /**
+     * Destroy the acceptor.
+     */
+    virtual ~Acceptor();
+
+    /**
+     * Start accepting a connection.
+     *
+     * If a connection is already accepted, but not retrieved yet, it
+     * returns true.
+     *
+     * If a new socket becomes immediately available, it returns true.
+     *
+     * If some error occurs, the acceptor is marked as failed.
+     */
+    bool accept();
+
+protected:
+    /**
+     * Called when a connection gets processed and we can become ready
+     * for the next one.
+     */
+    void connectionProcessed();
+};
+
+//------------------------------------------------------------------------------
+// Inline definitions
+//------------------------------------------------------------------------------
+
+inline Acceptor::Acceptor(ServerSocket* socket, size_t addressLength) :
+    socket(*socket),
+    acceptedFD(-1),
+    address( (addressLength==0) ? 0 : new unsigned char[addressLength]),
+    addressLength(addressLength)
+{
+}
+
+//------------------------------------------------------------------------------
+
+inline void Acceptor::connectionProcessed()
+{
+    acceptedFD = -1;
+}
+
+//------------------------------------------------------------------------------
+
+} /* namespace xplcommon::posix */ } /* namespace xplcommon */
+
+//------------------------------------------------------------------------------
+#endif // XPLCOMMON_POSIX_ACCEPTOR_H
+
+// Local Variables:
+// mode: C++
+// c-basic-offset: 4
+// indent-tabs-mode: nil
+// End:
Index: src/xplcommon/posix/BufferedWaitable.cc
===================================================================
--- src/xplcommon/posix/BufferedWaitable.cc	(revision 6)
+++ src/xplcommon/posix/BufferedWaitable.cc	(revision 6)
@@ -0,0 +1,90 @@
+// Copyright (c) 2012 by István Váradi
+
+// This file is part of libxplcommon, a common utility library for
+// development related to X-Plane
+
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+
+// 1. Redistributions of source code must retain the above copyright notice, this
+//    list of conditions and the following disclaimer.
+// 2. Redistributions in binary form must reproduce the above copyright notice,
+//    this list of conditions and the following disclaimer in the documentation
+//    and/or other materials provided with the distribution.
+
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// The views and conclusions contained in the software and documentation are those
+// of the authors and should not be interpreted as representing official policies,
+// either expressed or implied, of the FreeBSD Project.
+
+//------------------------------------------------------------------------------
+
+#include "BufferedWaitable.h"
+
+#include "ReadingBuffer.h"
+#include "WritingBuffer.h"
+
+#include <poll.h>
+
+//------------------------------------------------------------------------------
+
+using xplcommon::posix::BufferedWaitable;
+
+//------------------------------------------------------------------------------
+
+BufferedWaitable::BufferedWaitable(Waiter* waiter, int fd,
+                                   size_t readingCapacity,
+                                   size_t writingCapacity,
+                                   short events) :
+    Waitable(waiter, fd, events),
+    readingBuffer( (readingCapacity==0) ? 0 :
+                   new ReadingBuffer(readingCapacity, this)),
+    writingBuffer( (writingCapacity==0) ? 0 :
+                   new WritingBuffer(writingCapacity, this))
+{
+}
+
+//------------------------------------------------------------------------------
+
+BufferedWaitable::~BufferedWaitable()
+{
+    delete writingBuffer;
+    delete readingBuffer;
+}
+
+//------------------------------------------------------------------------------
+
+bool BufferedWaitable::isReady()
+{
+    return false;
+}
+
+//------------------------------------------------------------------------------
+
+void BufferedWaitable::handleEvents(short ev)
+{
+    if ((ev&POLLIN)==POLLIN && readingBuffer!=0) {
+        readingBuffer->read();
+    }
+    if ((ev&POLLOUT)==POLLOUT && writingBuffer!=0) {
+        writingBuffer->write();
+    }
+}
+
+//------------------------------------------------------------------------------
+
+// Local Variables:
+// mode: C++
+// c-basic-offset: 4
+// indent-tabs-mode: nil
+// End:
Index: src/xplcommon/posix/BufferedWaitable.h
===================================================================
--- src/xplcommon/posix/BufferedWaitable.h	(revision 6)
+++ src/xplcommon/posix/BufferedWaitable.h	(revision 6)
@@ -0,0 +1,155 @@
+// Copyright (c) 2012 by István Váradi
+
+// This file is part of libxplcommon, a common utility library for
+// development related to X-Plane
+
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+
+// 1. Redistributions of source code must retain the above copyright notice, this
+//    list of conditions and the following disclaimer.
+// 2. Redistributions in binary form must reproduce the above copyright notice,
+//    this list of conditions and the following disclaimer in the documentation
+//    and/or other materials provided with the distribution.
+
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// The views and conclusions contained in the software and documentation are those
+// of the authors and should not be interpreted as representing official policies,
+// either expressed or implied, of the FreeBSD Project.
+
+#ifndef XPLCOMMON_POSIX_BUFFEREDWAITABLE_H
+#define XPLCOMMON_POSIX_BUFFEREDWAITABLE_H
+//------------------------------------------------------------------------------
+
+#include "Waitable.h"
+
+#include <cstdlib>
+
+//------------------------------------------------------------------------------
+
+namespace xplcommon { namespace posix {
+
+//------------------------------------------------------------------------------
+
+class ReadingBuffer;
+class WritingBuffer;
+
+//------------------------------------------------------------------------------
+
+/**
+ * A waitable that has a read and/or a write buffer associated with
+ * it.
+ */
+class BufferedWaitable : public Waitable
+{
+public:
+    /**
+     * The default buffer capacity.
+     */
+    static const size_t DEFAULT_CAPACITY = 4096;
+
+private:
+    /**
+     * The reading buffer. It may be 0, if no reading buffer is
+     * associated with this waitable.
+     */
+    ReadingBuffer* readingBuffer;
+
+    /**
+     * The writing buffer. It may be 0, if no writing buffer is
+     * associated with this waitable.
+     */
+    WritingBuffer* writingBuffer;
+
+protected:
+    /**
+     * Construct the buffered waitable with the given capacities of
+     * the reading and writing buffers. If any of the capacities is 0,
+     * the corresponding buffer will not be created.
+     */
+    BufferedWaitable(Waiter* waiter = 0, int fd = -1,
+                     size_t readingCapacity = DEFAULT_CAPACITY,
+                     size_t writingCapacity = DEFAULT_CAPACITY,
+                     short events = 0);
+
+public:
+    /**
+     * Destroy the buffered waitable.
+     */
+    virtual ~BufferedWaitable();
+
+    /**
+     * Get the reference of the reading buffer. Note, that it can be a
+     * 0 reference, if the waitable has no reading buffer.
+     */
+    ReadingBuffer& getReadingBuffer();
+
+    /**
+     * Get the reference of the writing buffer. Note, that it can be a
+     * 0 reference, if the waitable has no writing buffer.
+     */
+    WritingBuffer& getWritingBuffer();
+
+protected:
+    /**
+     * Read using the file descriptor into the given buffer.
+     */
+    virtual ssize_t read(void* dest, size_t length) = 0;
+
+    /**
+     * Write using the file descriptor from the given buffer.
+     */
+    virtual ssize_t write(const void* src, size_t length) = 0;
+
+    /**
+     * Check if the waitable is ready.
+     */
+    virtual bool isReady();
+
+    /**
+     * Handle any events on the file descriptor.
+     */
+    virtual void handleEvents(short events);
+
+    friend class ReadingBuffer;
+    friend class WritingBuffer;
+};
+
+//------------------------------------------------------------------------------
+// Inline definitions
+//------------------------------------------------------------------------------
+
+inline ReadingBuffer& BufferedWaitable::getReadingBuffer()
+{
+    return *readingBuffer;
+}
+
+//------------------------------------------------------------------------------
+
+inline WritingBuffer& BufferedWaitable::getWritingBuffer()
+{
+    return *writingBuffer;
+}
+
+//------------------------------------------------------------------------------
+
+} /* namespace xplcommon::posix */ } /* namespace xplcommon */
+
+//------------------------------------------------------------------------------
+#endif // XPLCOMMON_POSIX_BUFFEREDWAITABLE_H
+
+// Local Variables:
+// mode: C++
+// c-basic-offset: 4
+// indent-tabs-mode: nil
+// End:
Index: src/xplcommon/posix/ClientSocket.cc
===================================================================
--- src/xplcommon/posix/ClientSocket.cc	(revision 6)
+++ src/xplcommon/posix/ClientSocket.cc	(revision 6)
@@ -0,0 +1,78 @@
+// Copyright (c) 2012 by István Váradi
+
+// This file is part of libxplcommon, a common utility library for
+// development related to X-Plane
+
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+
+// 1. Redistributions of source code must retain the above copyright notice, this
+//    list of conditions and the following disclaimer.
+// 2. Redistributions in binary form must reproduce the above copyright notice,
+//    this list of conditions and the following disclaimer in the documentation
+//    and/or other materials provided with the distribution.
+
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// The views and conclusions contained in the software and documentation are those
+// of the authors and should not be interpreted as representing official policies,
+// either expressed or implied, of the FreeBSD Project.
+
+//------------------------------------------------------------------------------
+
+#include "ClientSocket.h"
+
+#include "Connector.h"
+
+#include <poll.h>
+
+//------------------------------------------------------------------------------
+
+using xplcommon::posix::ClientSocket;
+using xplcommon::posix::Connector;
+
+//------------------------------------------------------------------------------
+
+ClientSocket::~ClientSocket()
+{
+    delete connector;
+}
+
+//------------------------------------------------------------------------------
+
+Connector& ClientSocket::getConnector()
+{
+    if (connector==0) {
+        connector = createConnector();
+    }
+    return *connector;
+}
+
+//------------------------------------------------------------------------------
+
+void ClientSocket::handleEvents(short events)
+{
+    if (connector!=0 && connector->connecting && (events&POLLOUT)==POLLOUT) {
+        connector->handleWritable();
+        events &= ~POLLOUT;
+    }
+
+    BufferedWaitable::handleEvents(events);
+}
+
+//------------------------------------------------------------------------------
+
+// Local Variables:
+// mode: C++
+// c-basic-offset: 4
+// indent-tabs-mode: nil
+// End:
Index: src/xplcommon/posix/ClientSocket.h
===================================================================
--- src/xplcommon/posix/ClientSocket.h	(revision 6)
+++ src/xplcommon/posix/ClientSocket.h	(revision 6)
@@ -0,0 +1,112 @@
+// Copyright (c) 2012 by István Váradi
+
+// This file is part of libxplcommon, a common utility library for
+// development related to X-Plane
+
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+
+// 1. Redistributions of source code must retain the above copyright notice, this
+//    list of conditions and the following disclaimer.
+// 2. Redistributions in binary form must reproduce the above copyright notice,
+//    this list of conditions and the following disclaimer in the documentation
+//    and/or other materials provided with the distribution.
+
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// The views and conclusions contained in the software and documentation are those
+// of the authors and should not be interpreted as representing official policies,
+// either expressed or implied, of the FreeBSD Project.
+
+#ifndef XPLCOMMON_POSIX_CLIENTSOCKET_H
+#define XPLCOMMON_POSIX_CLIENTSOCKET_H
+//------------------------------------------------------------------------------
+
+#include "Socket.h"
+
+//------------------------------------------------------------------------------
+
+namespace xplcommon { namespace posix {
+
+//------------------------------------------------------------------------------
+
+/**
+ * A socket that can be connected to another one.
+ */
+class ClientSocket : public Socket
+{
+private:
+    /**
+     * The connector, if any. It is created only, if requested.
+     */
+    Connector* connector;
+
+protected:
+    /**
+     * Construct the socket with the given parameters.
+     */
+    ClientSocket(int domain, int type, Waiter* waiter = 0, int protocol = 0,
+                 size_t readingCapacity = DEFAULT_CAPACITY,
+                 size_t writingCapacity = DEFAULT_CAPACITY);
+
+public:
+    /**
+     * Destroy the socket.
+     */
+    virtual ~ClientSocket();
+
+    /**
+     * Get the connector. Use this object to connect to another socket.
+     */
+    Connector& getConnector();
+
+protected:
+    /**
+     * Create a new connector of the correct type.
+     */
+    virtual Connector* createConnector() = 0;
+
+    /**
+     * Handle any events on the file descriptor. If a connection is in
+     * progress, and the incoming events have the EPOLLOUT flag set,
+     * the connector is called to handle the event. Then the parent
+     * class' handleEvents function is called.
+     */
+    virtual void handleEvents(short events);
+
+    friend class Connector;
+};
+
+//------------------------------------------------------------------------------
+// Inline definitions
+//------------------------------------------------------------------------------
+
+inline ClientSocket::ClientSocket(int domain, int type, Waiter* waiter,
+                                  int protocol, size_t readingCapacity,
+                                  size_t writingCapacity) :
+    Socket(domain, type, waiter, protocol, readingCapacity, writingCapacity),
+    connector(0)
+{
+}
+
+//------------------------------------------------------------------------------
+
+} /* namespace xplcommon::posix */ } /* namespace xplcommon */
+
+//------------------------------------------------------------------------------
+#endif // XPLCOMMON_POSIX_CLIENTSOCKET_H
+
+// Local Variables:
+// mode: C++
+// c-basic-offset: 4
+// indent-tabs-mode: nil
+// End:
Index: src/xplcommon/posix/Connector.cc
===================================================================
--- src/xplcommon/posix/Connector.cc	(revision 6)
+++ src/xplcommon/posix/Connector.cc	(revision 6)
@@ -0,0 +1,98 @@
+// Copyright (c) 2012 by István Váradi
+
+// This file is part of libxplcommon, a common utility library for
+// development related to X-Plane
+
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+
+// 1. Redistributions of source code must retain the above copyright notice, this
+//    list of conditions and the following disclaimer.
+// 2. Redistributions in binary form must reproduce the above copyright notice,
+//    this list of conditions and the following disclaimer in the documentation
+//    and/or other materials provided with the distribution.
+
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// The views and conclusions contained in the software and documentation are those
+// of the authors and should not be interpreted as representing official policies,
+// either expressed or implied, of the FreeBSD Project.
+
+//------------------------------------------------------------------------------
+
+#include "Connector.h"
+
+#include "ClientSocket.h"
+
+#include <cerrno>
+
+#include <poll.h>
+#include <sys/socket.h>
+
+//------------------------------------------------------------------------------
+
+using xplcommon::posix::Connector;
+
+//------------------------------------------------------------------------------
+
+bool Connector::connect()
+{
+    if (connected) return true;
+    if (connecting || failed()) return false;
+
+    size_t addrlen = 0;
+    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);
+        }
+    } else {
+        connected = true;
+    }
+
+    return result==0;
+}
+
+//------------------------------------------------------------------------------
+
+void Connector::handleWritable()
+{
+    if (!connecting) return;
+
+    connecting = false;
+    socket.events &= ~POLLOUT;
+
+    int error = 0;
+    socklen_t len = sizeof(error);
+    if (getsockopt(socket.fd, SOL_SOCKET, SO_ERROR, &error, &len)<0) {
+        setErrorCode(errno);
+    } else {
+        if (error==0) {
+            connected = true;
+        } else {
+            setErrorCode(error);
+        }
+    }
+}
+
+//------------------------------------------------------------------------------
+
+// Local Variables:
+// mode: C++
+// c-basic-offset: 4
+// indent-tabs-mode: nil
+// End:
Index: src/xplcommon/posix/Connector.h
===================================================================
--- src/xplcommon/posix/Connector.h	(revision 6)
+++ src/xplcommon/posix/Connector.h	(revision 6)
@@ -0,0 +1,139 @@
+// Copyright (c) 2012 by István Váradi
+
+// This file is part of libxplcommon, a common utility library for
+// development related to X-Plane
+
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+
+// 1. Redistributions of source code must retain the above copyright notice, this
+//    list of conditions and the following disclaimer.
+// 2. Redistributions in binary form must reproduce the above copyright notice,
+//    this list of conditions and the following disclaimer in the documentation
+//    and/or other materials provided with the distribution.
+
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// The views and conclusions contained in the software and documentation are those
+// of the authors and should not be interpreted as representing official policies,
+// either expressed or implied, of the FreeBSD Project.
+
+#ifndef XPLCOMMON_POSIX_CONNECTOR_H
+#define XPLCOMMON_POSIX_CONNECTOR_H
+//------------------------------------------------------------------------------
+
+#include "../Failable.h"
+
+#include <cstdlib>
+
+//------------------------------------------------------------------------------
+
+struct sockaddr;
+
+//------------------------------------------------------------------------------
+
+namespace xplcommon { namespace posix {
+
+//------------------------------------------------------------------------------
+
+class ClientSocket;
+
+//------------------------------------------------------------------------------
+
+/**
+ * Class representing the creation of a connection to some remote
+ * entity, like a socket or a pipe.
+ */
+class Connector : public Failable
+{
+private:
+    /**
+     * The socket on behalf of which the connection is being
+     * performed.
+     */
+    ClientSocket& socket;
+
+    /**
+     * Indicate that we are in the process of connecting.
+     */
+    bool connecting;
+
+    /**
+     * Indicate that we are connected.
+     */
+    bool connected;
+
+protected:
+    /**
+     * Construct the connector.
+     */
+    Connector(ClientSocket* socket);
+
+public:
+    /**
+     * Connect to an address properly set up.
+     *
+     * If the connection succeeds immediately, it indicates success.
+     *
+     * If the connection does not succeed, because it would block, it
+     * sets up the socket to wait for the connection to succeed, and
+     * returns failure.
+     *
+     * If the connection fails for another reason, the error code is
+     * set, and false is returned.
+     */
+    bool connect();
+
+protected:
+    /**
+     * Get the parameters for connecting.
+     */
+    virtual const struct sockaddr* getAddress(size_t& addrlen) = 0;
+
+    /**
+     * Handle the event that the socket is writable.
+     *
+     * If connection is in progress, this function returns
+     * immediately.
+     *
+     * Otherwise it reads the error code using getsockopt(), and if it
+     * indicates success, the connector is set to connected, otherwise
+     * it is set to failed.
+     */
+    void handleWritable();
+
+    friend class ClientSocket;
+};
+
+//------------------------------------------------------------------------------
+// Inline definitions
+//------------------------------------------------------------------------------
+
+inline Connector::Connector(ClientSocket* socket) :
+    socket(*socket),
+    connecting(false),
+    connected(false)
+{
+}
+
+//------------------------------------------------------------------------------
+
+} /* namespace xplcommon::posix */ } /* namespace xplcommon */
+
+//------------------------------------------------------------------------------
+#endif // XPLCOMMON_POSIX_CONNECTOR_H
+
+// Local Variables:
+// mode: C++
+// c-basic-offset: 4
+// indent-tabs-mode: nil
+// End:
Index: src/xplcommon/posix/Makefile.am
===================================================================
--- src/xplcommon/posix/Makefile.am	(revision 4)
+++ src/xplcommon/posix/Makefile.am	(revision 6)
@@ -3,7 +3,15 @@
 libxplcommon_posix_la_SOURCES =	\
 	Thread.cc		\
-	Waitable.cc	\
-	Waiter.cc	\
-	WaitableEvent.cc
+	Waitable.cc		\
+	Waiter.cc		\
+	WaitableEvent.cc	\
+	ReadingBuffer.cc	\
+	WritingBuffer.cc	\
+	BufferedWaitable.cc	\
+	Acceptor.cc		\
+	ServerSocket.cc		\
+	Socket.cc		\
+	Connector.cc		\
+	ClientSocket.cc
 
 include_xplcommon_posixdir=$(includedir)/xplcommon/posix
@@ -12,3 +20,12 @@
 	Waitable.h		\
 	Waiter.h		\
-	WaitableEvent.h
+	WaitableEvent.h		\
+	WaitableBuffer.h	\
+	ReadingBuffer.h		\
+	WritingBuffer.h		\
+	BufferedWaitable.h	\
+	Acceptor.h		\
+	ServerSocket.h		\
+	Socket.h		\
+	Connector.h		\
+	ClientSocket.h
Index: src/xplcommon/posix/ReadingBuffer.cc
===================================================================
--- src/xplcommon/posix/ReadingBuffer.cc	(revision 6)
+++ src/xplcommon/posix/ReadingBuffer.cc	(revision 6)
@@ -0,0 +1,76 @@
+// Copyright (c) 2012 by István Váradi
+
+// This file is part of libxplcommon, a common utility library for
+// development related to X-Plane
+
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+
+// 1. Redistributions of source code must retain the above copyright notice, this
+//    list of conditions and the following disclaimer.
+// 2. Redistributions in binary form must reproduce the above copyright notice,
+//    this list of conditions and the following disclaimer in the documentation
+//    and/or other materials provided with the distribution.
+
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// The views and conclusions contained in the software and documentation are those
+// of the authors and should not be interpreted as representing official policies,
+// either expressed or implied, of the FreeBSD Project.
+
+//------------------------------------------------------------------------------
+
+#include "ReadingBuffer.h"
+
+#include "BufferedWaitable.h"
+
+#include <cerrno>
+
+#include <poll.h>
+
+//------------------------------------------------------------------------------
+
+using xplcommon::posix::ReadingBuffer;
+
+//------------------------------------------------------------------------------
+
+bool ReadingBuffer::read()
+{
+    if (failed()) return false;
+
+    if (getLength()>0) {
+        waitable.events &= ~POLLIN;
+        return true;
+    }
+
+    ssize_t result = waitable.read(getData(), getCapacity());
+    if (result<0) {
+        if (errno==EAGAIN || errno==EWOULDBLOCK) {
+            waitable.events |= POLLIN;
+        } else {
+            waitable.events &= ~POLLIN;
+            setErrorCode(errno);
+        }
+    } else {
+        waitable.events &= ~POLLIN;
+        addLength(result);
+    }
+    return result>=0;
+}
+
+//------------------------------------------------------------------------------
+
+// Local Variables:
+// mode: C++
+// c-basic-offset: 4
+// indent-tabs-mode: nil
+// End:
Index: src/xplcommon/posix/ReadingBuffer.h
===================================================================
--- src/xplcommon/posix/ReadingBuffer.h	(revision 6)
+++ src/xplcommon/posix/ReadingBuffer.h	(revision 6)
@@ -0,0 +1,102 @@
+// Copyright (c) 2012 by István Váradi
+
+// This file is part of libxplcommon, a common utility library for
+// development related to X-Plane
+
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+
+// 1. Redistributions of source code must retain the above copyright notice, this
+//    list of conditions and the following disclaimer.
+// 2. Redistributions in binary form must reproduce the above copyright notice,
+//    this list of conditions and the following disclaimer in the documentation
+//    and/or other materials provided with the distribution.
+
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// The views and conclusions contained in the software and documentation are those
+// of the authors and should not be interpreted as representing official policies,
+// either expressed or implied, of the FreeBSD Project.
+
+#ifndef XPLCOMMON_POSIX_READINGBUFFER_H
+#define XPLCOMMON_POSIX_READINGBUFFER_H
+//------------------------------------------------------------------------------
+
+#include "WaitableBuffer.h"
+
+//------------------------------------------------------------------------------
+
+namespace xplcommon { namespace posix {
+
+//------------------------------------------------------------------------------
+
+/**
+ * A buffer used for reading.
+ *
+ * It should be made empty, and then the function read() should be
+ * called on it. If it returns true, some data could be read. If the
+ * amount of data read is 0, that indicates and end-of-file condition.
+ */
+class ReadingBuffer : public WaitableBuffer
+{
+protected:
+    /**
+     * Construct the buffer.
+     */
+    ReadingBuffer(size_t capacity, BufferedWaitable* waitable);
+
+public:
+    /**
+     * Read into the buffer using the buffered waitable if possible.
+     *
+     * If the buffer contains data, this function returns immediately
+     * with success, so the buffer should be reset before reading.
+     *
+     * If some data can be read immediately, that is stored in the
+     * buffer, and success is indicated. Success is indicated even if
+     * the amount of data read is 0, allowing for signalling the
+     * end-of-file or end-of-connection condition.
+     *
+     * If the reading operation would block, the waitable is set up so
+     * that it would read, and failure is returned.
+     *
+     * Otherwise, if the reading operation fails for some other
+     * reason, the buffer's error code is set, so it should always be
+     * checked on a failure!
+     */
+    bool read();
+
+    friend class BufferedWaitable;
+};
+
+//------------------------------------------------------------------------------
+// Inline definitions
+//------------------------------------------------------------------------------
+
+inline ReadingBuffer::ReadingBuffer(size_t capacity,
+                                    BufferedWaitable* waitable) :
+    WaitableBuffer(capacity, waitable)
+{
+}
+
+//------------------------------------------------------------------------------
+
+} /* namespace xplcommon::posix */ } /* namespace xplcommon */
+
+//------------------------------------------------------------------------------
+#endif // XPLCOMMON_POSIX_READINGBUFFER_H
+
+// Local Variables:
+// mode: C++
+// c-basic-offset: 4
+// indent-tabs-mode: nil
+// End:
Index: src/xplcommon/posix/ServerSocket.cc
===================================================================
--- src/xplcommon/posix/ServerSocket.cc	(revision 6)
+++ src/xplcommon/posix/ServerSocket.cc	(revision 6)
@@ -0,0 +1,89 @@
+// Copyright (c) 2012 by István Váradi
+
+// This file is part of libxplcommon, a common utility library for
+// development related to X-Plane
+
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+
+// 1. Redistributions of source code must retain the above copyright notice, this
+//    list of conditions and the following disclaimer.
+// 2. Redistributions in binary form must reproduce the above copyright notice,
+//    this list of conditions and the following disclaimer in the documentation
+//    and/or other materials provided with the distribution.
+
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// The views and conclusions contained in the software and documentation are those
+// of the authors and should not be interpreted as representing official policies,
+// either expressed or implied, of the FreeBSD Project.
+
+//------------------------------------------------------------------------------
+
+#include "ServerSocket.h"
+
+#include "Socket.h"
+
+#include <cstdio>
+
+#include <unistd.h>
+#include <sys/socket.h>
+
+//------------------------------------------------------------------------------
+
+using xplcommon::posix::ServerSocket;
+using xplcommon::posix::Acceptor;
+
+//------------------------------------------------------------------------------
+
+ServerSocket::ServerSocket(int domain, int type, Waiter* waiter, int protocol) :
+    Waitable(waiter, Socket::socket(domain, type, protocol))
+{
+}
+
+//------------------------------------------------------------------------------
+
+ServerSocket::~ServerSocket()
+{
+    ::close(fd);
+}
+
+//------------------------------------------------------------------------------
+
+int 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;
+        }
+    }
+
+    return ::bind(fd, addr, addrlen);
+}
+
+//------------------------------------------------------------------------------
+
+int ServerSocket::listen(int backlog)
+{
+    return ::listen(fd, backlog);
+}
+
+//------------------------------------------------------------------------------
+
+// Local Variables:
+// mode: C++
+// c-basic-offset: 4
+// indent-tabs-mode: nil
+// End:
Index: src/xplcommon/posix/ServerSocket.h
===================================================================
--- src/xplcommon/posix/ServerSocket.h	(revision 6)
+++ src/xplcommon/posix/ServerSocket.h	(revision 6)
@@ -0,0 +1,100 @@
+// Copyright (c) 2012 by István Váradi
+
+// This file is part of libxplcommon, a common utility library for
+// development related to X-Plane
+
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+
+// 1. Redistributions of source code must retain the above copyright notice, this
+//    list of conditions and the following disclaimer.
+// 2. Redistributions in binary form must reproduce the above copyright notice,
+//    this list of conditions and the following disclaimer in the documentation
+//    and/or other materials provided with the distribution.
+
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// The views and conclusions contained in the software and documentation are those
+// of the authors and should not be interpreted as representing official policies,
+// either expressed or implied, of the FreeBSD Project.
+
+#ifndef XPLCOMMON_POSIX_SERVERSOCKET_H
+#define XPLCOMMON_POSIX_SERVERSOCKET_H
+//------------------------------------------------------------------------------
+
+#include "Waitable.h"
+
+#include <cstdlib>
+
+//------------------------------------------------------------------------------
+
+struct sockaddr;
+
+//------------------------------------------------------------------------------
+
+namespace xplcommon { namespace posix {
+
+//------------------------------------------------------------------------------
+
+class Acceptor;
+
+//------------------------------------------------------------------------------
+
+/**
+ * A socket in a server role. It can only bind to a new address,
+ * listen and accept connections. It cannot connect to other sockets
+ * and cannot be read or written.
+ */
+class ServerSocket : public Waitable
+{
+protected:
+    /**
+     * Construct the server socket.
+     */
+    ServerSocket(int domain, int type, Waiter* waiter = 0, int protocol = 0);
+
+public:
+    /**
+     * Destroy the server socket.
+     */
+    virtual ~ServerSocket();
+
+protected:
+    /**
+     * Bind the socket to the given address.
+     *
+     * @param reuseaddr if set to true, the SO_REUSEADDR socket option
+     * will be set.
+     */
+    int bind(const struct sockaddr* addr, size_t addrlen,
+             bool reuseaddr = true);
+
+    /**
+     * Start listening on the socket.
+     */
+    int listen(int backlog = 5);
+
+    friend class Acceptor;
+};
+
+//------------------------------------------------------------------------------
+
+} /* namespace xplcommon::posix */ } /* namespace xplcommon */
+
+//------------------------------------------------------------------------------
+#endif // XPLCOMMON_POSIX_SERVERSOCKET_H
+
+// Local Variables:
+// mode: C++
+// c-basic-offset: 4
+// indent-tabs-mode: nil
+// End:
Index: src/xplcommon/posix/Socket.cc
===================================================================
--- src/xplcommon/posix/Socket.cc	(revision 6)
+++ src/xplcommon/posix/Socket.cc	(revision 6)
@@ -0,0 +1,67 @@
+// Copyright (c) 2012 by István Váradi
+
+// This file is part of libxplcommon, a common utility library for
+// development related to X-Plane
+
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+
+// 1. Redistributions of source code must retain the above copyright notice, this
+//    list of conditions and the following disclaimer.
+// 2. Redistributions in binary form must reproduce the above copyright notice,
+//    this list of conditions and the following disclaimer in the documentation
+//    and/or other materials provided with the distribution.
+
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// The views and conclusions contained in the software and documentation are those
+// of the authors and should not be interpreted as representing official policies,
+// either expressed or implied, of the FreeBSD Project.
+
+//------------------------------------------------------------------------------
+
+#include "Socket.h"
+
+#include <cstdio>
+
+#include <unistd.h>
+#include <sys/socket.h>
+
+//------------------------------------------------------------------------------
+
+using xplcommon::posix::Socket;
+
+//------------------------------------------------------------------------------
+
+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;
+}
+
+//------------------------------------------------------------------------------
+
+Socket::~Socket()
+{
+    ::close(fd);
+}
+
+//------------------------------------------------------------------------------
+
+// Local Variables:
+// mode: C++
+// c-basic-offset: 4
+// indent-tabs-mode: nil
+// End:
Index: src/xplcommon/posix/Socket.h
===================================================================
--- src/xplcommon/posix/Socket.h	(revision 6)
+++ src/xplcommon/posix/Socket.h	(revision 6)
@@ -0,0 +1,100 @@
+// Copyright (c) 2012 by István Váradi
+
+// This file is part of libxplcommon, a common utility library for
+// development related to X-Plane
+
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+
+// 1. Redistributions of source code must retain the above copyright notice, this
+//    list of conditions and the following disclaimer.
+// 2. Redistributions in binary form must reproduce the above copyright notice,
+//    this list of conditions and the following disclaimer in the documentation
+//    and/or other materials provided with the distribution.
+
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// The views and conclusions contained in the software and documentation are those
+// of the authors and should not be interpreted as representing official policies,
+// either expressed or implied, of the FreeBSD Project.
+
+#ifndef XPLCOMMON_POSIX_SOCKET_H
+#define XPLCOMMON_POSIX_SOCKET_H
+//------------------------------------------------------------------------------
+
+#include "BufferedWaitable.h"
+
+//------------------------------------------------------------------------------
+
+struct sockaddr;
+
+//------------------------------------------------------------------------------
+
+namespace xplcommon { namespace posix {
+
+//------------------------------------------------------------------------------
+
+class Connector;
+
+//------------------------------------------------------------------------------
+
+/**
+ * A buffered, waitable socket implementation.
+ */
+class Socket : public BufferedWaitable
+{
+private:
+    /**
+     * Construct a socket with the given parameters.
+     */
+    static int socket(int domain, int type, int protocol);
+
+protected:
+    /**
+     * Construct the socket with the given parameters.
+     */
+    Socket(int domain, int type, Waiter* waiter = 0, int protocol = 0,
+           size_t readingCapacity = DEFAULT_CAPACITY,
+           size_t writingCapacity = DEFAULT_CAPACITY);
+
+public:
+    /**
+     * Destroy the socket by closing the file descriptor.
+     */
+    virtual ~Socket();
+
+    friend class ServerSocket;
+};
+
+//------------------------------------------------------------------------------
+// Inline definitions
+//------------------------------------------------------------------------------
+
+inline Socket::Socket(int domain, int type, Waiter* waiter, int protocol,
+                      size_t readingCapacity, size_t writingCapacity) :
+    BufferedWaitable(waiter, socket(domain, type, protocol),
+                     readingCapacity, writingCapacity)
+{
+}
+
+//------------------------------------------------------------------------------
+
+} /* namespace xplcommon::posix */ } /* namespace xplcommon */
+
+//------------------------------------------------------------------------------
+#endif // XPLCOMMON_POSIX_SOCKET_H
+
+// Local Variables:
+// mode: C++
+// c-basic-offset: 4
+// indent-tabs-mode: nil
+// End:
Index: src/xplcommon/posix/Waitable.h
===================================================================
--- src/xplcommon/posix/Waitable.h	(revision 4)
+++ src/xplcommon/posix/Waitable.h	(revision 6)
@@ -82,13 +82,30 @@
 
     /**
+     * Indicate if this waitable is valid, i.e. the file descriptor is
+     * non-negative.
+     */
+    bool isValid() const;
+
+    /**
+     * Determine if the waitable is ready or not, i.e. if there is no
+     * need to wait for it.
+     *
+     * It first checks the events. If they are 0, the waitable is
+     * ready. Then it calls isReady() and returns the result of that
+     * function.
+     */
+    bool ready();
+
+protected:
+    /**
      * Indicate if the waitable is ready, i.e. there is no need to
      * wait for it.
      *
-     * Calling this function should not deleted the ready
-     * indication. It should be deleted by actually processing the data.
+     * Calling this function should not delete the ready
+     * indication. It should be deleted by actually processing the
+     * data.
      */
-    virtual bool ready() = 0;
+    virtual bool isReady() = 0;
 
-protected:
     /**
      * Handle the given events.
@@ -98,4 +115,20 @@
     friend class Waiter;
 };
+
+//------------------------------------------------------------------------------
+// Inline defintions
+//------------------------------------------------------------------------------
+
+inline bool Waitable::isValid() const
+{
+    return fd>=0;
+}
+
+//------------------------------------------------------------------------------
+
+inline bool Waitable::ready()
+{
+    return (events==0) || isReady();
+}
 
 //------------------------------------------------------------------------------
Index: src/xplcommon/posix/WaitableBuffer.h
===================================================================
--- src/xplcommon/posix/WaitableBuffer.h	(revision 6)
+++ src/xplcommon/posix/WaitableBuffer.h	(revision 6)
@@ -0,0 +1,88 @@
+// Copyright (c) 2012 by István Váradi
+
+// This file is part of libxplcommon, a common utility library for
+// development related to X-Plane
+
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+
+// 1. Redistributions of source code must retain the above copyright notice, this
+//    list of conditions and the following disclaimer.
+// 2. Redistributions in binary form must reproduce the above copyright notice,
+//    this list of conditions and the following disclaimer in the documentation
+//    and/or other materials provided with the distribution.
+
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// The views and conclusions contained in the software and documentation are those
+// of the authors and should not be interpreted as representing official policies,
+// either expressed or implied, of the FreeBSD Project.
+
+#ifndef XPLCOMMON_POSIX_WAITABLEBUFFER_H
+#define XPLCOMMON_POSIX_WAITABLEBUFFER_H
+//------------------------------------------------------------------------------
+
+#include "../Buffer.h"
+#include "../Failable.h"
+
+//------------------------------------------------------------------------------
+
+namespace xplcommon { namespace posix {
+
+//------------------------------------------------------------------------------
+
+class BufferedWaitable;
+
+//------------------------------------------------------------------------------
+
+/**
+ * Base class for buffers used by a BufferedWaitable.
+ */
+class WaitableBuffer : public ::xplcommon::Buffer,
+                       public ::xplcommon::Failable
+{
+protected:
+    /**
+     * Our waitable.
+     */
+    BufferedWaitable& waitable;
+
+protected:
+    /**
+     * Construct the buffer with the given waitable.
+     */
+    WaitableBuffer(size_t capacity, BufferedWaitable* waitable);
+};
+
+//------------------------------------------------------------------------------
+// Inline definitions
+//------------------------------------------------------------------------------
+
+inline WaitableBuffer::WaitableBuffer(size_t capacity,
+                                      BufferedWaitable* waitable) :
+    Buffer(capacity),
+    waitable(*waitable)
+{
+}
+
+//------------------------------------------------------------------------------
+
+} /* namespace xplcommon::posix */ } /* namespace xplcommon */
+
+//------------------------------------------------------------------------------
+#endif // XPLCOMMON_POSIX_WAITABLEBUFFER_H
+
+// Local Variables:
+// mode: C++
+// c-basic-offset: 4
+// indent-tabs-mode: nil
+// End:
Index: src/xplcommon/posix/WaitableEvent.cc
===================================================================
--- src/xplcommon/posix/WaitableEvent.cc	(revision 4)
+++ src/xplcommon/posix/WaitableEvent.cc	(revision 6)
@@ -62,11 +62,4 @@
 //------------------------------------------------------------------------------
 
-bool WaitableEvent::ready()
-{
-    return fired;
-}
-
-//------------------------------------------------------------------------------
-
 void WaitableEvent::fire()
 {
@@ -90,4 +83,11 @@
         return false;
     }
+}
+
+//------------------------------------------------------------------------------
+
+bool WaitableEvent::isReady()
+{
+    return fired;
 }
 
Index: src/xplcommon/posix/WaitableEvent.h
===================================================================
--- src/xplcommon/posix/WaitableEvent.h	(revision 4)
+++ src/xplcommon/posix/WaitableEvent.h	(revision 6)
@@ -63,9 +63,4 @@
 
     /**
-     * Check if the event is fired or not.
-     */
-    virtual bool ready();
-
-    /**
      * Fire the event.
      */
@@ -79,4 +74,9 @@
 
 protected:
+    /**
+     * Check if the event is fired or not.
+     */
+    virtual bool isReady();
+
     /**
      * Handle the given events.
Index: src/xplcommon/posix/WritingBuffer.cc
===================================================================
--- src/xplcommon/posix/WritingBuffer.cc	(revision 6)
+++ src/xplcommon/posix/WritingBuffer.cc	(revision 6)
@@ -0,0 +1,88 @@
+// Copyright (c) 2012 by István Váradi
+
+// This file is part of libxplcommon, a common utility library for
+// development related to X-Plane
+
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+
+// 1. Redistributions of source code must retain the above copyright notice, this
+//    list of conditions and the following disclaimer.
+// 2. Redistributions in binary form must reproduce the above copyright notice,
+//    this list of conditions and the following disclaimer in the documentation
+//    and/or other materials provided with the distribution.
+
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// The views and conclusions contained in the software and documentation are those
+// of the authors and should not be interpreted as representing official policies,
+// either expressed or implied, of the FreeBSD Project.
+
+//------------------------------------------------------------------------------
+
+#include "WritingBuffer.h"
+
+#include "BufferedWaitable.h"
+
+#include <cerrno>
+
+#include <poll.h>
+
+//------------------------------------------------------------------------------
+
+using xplcommon::posix::WritingBuffer;
+
+//------------------------------------------------------------------------------
+
+void WritingBuffer::reset()
+{
+    Buffer::reset();
+    offset = 0;
+    waitable.events &= ~POLLOUT;
+}
+
+//------------------------------------------------------------------------------
+
+bool WritingBuffer::write()
+{
+    if (failed()) return false;
+
+    if (getLength()<=offset) {
+        reset();
+        return true;
+    }
+
+    size_t toWrite = getLength() - offset;
+
+    ssize_t result = waitable.write(getData() + offset, toWrite);
+    if (result<static_cast<ssize_t>(toWrite)) {
+        if (result>=0 || errno==EAGAIN || errno==EWOULDBLOCK) {
+            waitable.events |= POLLOUT;
+            if (result>0) offset += result;
+        } else {
+            waitable.events &= ~POLLOUT;
+            setErrorCode(errno);
+        }
+        return false;
+    } else {
+        reset();
+        return true;
+    }
+}
+
+//------------------------------------------------------------------------------
+
+// Local Variables:
+// mode: C++
+// c-basic-offset: 4
+// indent-tabs-mode: nil
+// End:
Index: src/xplcommon/posix/WritingBuffer.h
===================================================================
--- src/xplcommon/posix/WritingBuffer.h	(revision 6)
+++ src/xplcommon/posix/WritingBuffer.h	(revision 6)
@@ -0,0 +1,115 @@
+// Copyright (c) 2012 by István Váradi
+
+// This file is part of libxplcommon, a common utility library for
+// development related to X-Plane
+
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+
+// 1. Redistributions of source code must retain the above copyright notice, this
+//    list of conditions and the following disclaimer.
+// 2. Redistributions in binary form must reproduce the above copyright notice,
+//    this list of conditions and the following disclaimer in the documentation
+//    and/or other materials provided with the distribution.
+
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// The views and conclusions contained in the software and documentation are those
+// of the authors and should not be interpreted as representing official policies,
+// either expressed or implied, of the FreeBSD Project.
+
+#ifndef XPLCOMMON_POSIX_WRITINGBUFFER_H
+#define XPLCOMMON_POSIX_WRITINGBUFFER_H
+//------------------------------------------------------------------------------
+
+#include "WaitableBuffer.h"
+
+//------------------------------------------------------------------------------
+
+namespace xplcommon { namespace posix {
+
+//------------------------------------------------------------------------------
+
+/**
+ * A buffer used for writing.
+ *
+ * The data to write should be stored in the buffer. Then write()
+ * should be called. If it returns true, it indicates that all data
+ * has been written. In that case the buffer is automatically emptied.
+ */
+class WritingBuffer : public WaitableBuffer
+{
+private:
+    /**
+     * The offset of the next byte to write.
+     */
+    size_t offset;
+
+protected:
+    /**
+     * Construct the buffer.
+     */
+    WritingBuffer(size_t capacity, BufferedWaitable* waitable);
+
+public:
+    /**
+     * Reset the buffer. It resets the offset too.
+     */
+    void reset();
+
+    /**
+     * Write the contents of the buffer using the associated buffered
+     * waitable.
+     *
+     * If the buffer is empty, the function returns indicating
+     * success.
+     *
+     * If the complete remaining contents could be written, it also indicates
+     * success, and the buffer is reset, ready to be used again.
+     *
+     * If not everything can be written and the write operation
+     * indicates that it would block, this function returns failure
+     * after the waitable has been set up  to handle the corresponding
+     * poll events.
+     *
+     * Otherwise, if the writing operation fails for some other
+     * reason, the buffer's error code is set to the error's code and
+     * false is returned.
+     */
+    bool write();
+
+    friend class BufferedWaitable;
+};
+
+//------------------------------------------------------------------------------
+// Inline definitions
+//------------------------------------------------------------------------------
+
+inline WritingBuffer::WritingBuffer(size_t capacity,
+                                    BufferedWaitable* waitable):
+    WaitableBuffer(capacity, waitable),
+    offset(0)
+{
+}
+
+//------------------------------------------------------------------------------
+
+} /* namespace xplcommon::posix */ } /* namespace xplcommon */
+
+//------------------------------------------------------------------------------
+#endif // XPLCOMMON_POSIX_WRITINGBUFFER_H
+
+// Local Variables:
+// mode: C++
+// c-basic-offset: 4
+// indent-tabs-mode: nil
+// End:
Index: src/xplcommon/types.h
===================================================================
--- src/xplcommon/types.h	(revision 6)
+++ src/xplcommon/types.h	(revision 6)
@@ -0,0 +1,77 @@
+// Copyright (c) 2012 by István Váradi
+
+// This file is part of libxplcommon, a common utility library for
+// development related to X-Plane
+
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+
+// 1. Redistributions of source code must retain the above copyright notice, this
+//    list of conditions and the following disclaimer.
+// 2. Redistributions in binary form must reproduce the above copyright notice,
+//    this list of conditions and the following disclaimer in the documentation
+//    and/or other materials provided with the distribution.
+
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// The views and conclusions contained in the software and documentation are those
+// of the authors and should not be interpreted as representing official policies,
+// either expressed or implied, of the FreeBSD Project.
+
+#ifndef XPLCOMMON_TYPES_H
+#define XPLCOMMON_TYPES_H
+//------------------------------------------------------------------------------
+
+#include "config.h"
+
+#if TARGET_API_WIN32
+#include <windows.h>
+#endif
+
+//------------------------------------------------------------------------------
+
+namespace xplcommon {
+
+//------------------------------------------------------------------------------
+
+#if TARGET_API_WIN32
+
+//------------------------------------------------------------------------------
+
+typedef DWORD errorCode_t;
+static const DWORD NO_ERROR = 0;
+
+//------------------------------------------------------------------------------
+
+#else // TARGET_API_WIN32
+
+//------------------------------------------------------------------------------
+
+typedef int errorCode_t;
+static const int NO_ERROR = 0;
+
+//------------------------------------------------------------------------------
+
+#endif // TARGET_API_WIN32
+
+//------------------------------------------------------------------------------
+
+} /* namespace xplcommon */
+
+//------------------------------------------------------------------------------
+#endif // XPLCOMMON_TYPES_H
+
+// Local Variables:
+// mode: C++
+// c-basic-offset: 4
+// indent-tabs-mode: nil
+// End:
