Index: /trunk/src/network/kernel/qhostinfo_os2.cpp
===================================================================
--- /trunk/src/network/kernel/qhostinfo_os2.cpp	(revision 567)
+++ /trunk/src/network/kernel/qhostinfo_os2.cpp	(revision 568)
@@ -69,9 +69,4 @@
 
 QT_BEGIN_NAMESPACE
-
-// Almost always the same. If not, specify in qplatformdefs.h.
-#if !defined(QT_SOCKOPTLEN_T)
-# define QT_SOCKOPTLEN_T QT_SOCKLEN_T
-#endif
 
 QHostInfo QHostInfoAgent::fromName(const QString &hostName)
Index: /trunk/src/network/socket/qlocalserver_os2.cpp
===================================================================
--- /trunk/src/network/socket/qlocalserver_os2.cpp	(revision 567)
+++ /trunk/src/network/socket/qlocalserver_os2.cpp	(revision 568)
@@ -91,5 +91,5 @@
 
     // create the unix socket
-    listenSocket = qSocket(PF_UNIX, SOCK_STREAM, 0);
+    listenSocket = ::socket(PF_UNIX, SOCK_STREAM, 0);
     if (-1 == listenSocket) {
         setError(QLatin1String("QLocalServer::listen"));
@@ -110,5 +110,5 @@
 
     // bind
-    if(-1 == qBind(listenSocket, (sockaddr *)&addr, sizeof(sockaddr_un))) {
+    if(-1 == ::bind(listenSocket, (sockaddr *)&addr, sizeof(sockaddr_un))) {
         setError(QLatin1String("QLocalServer::listen"));
         // if address is in use already, just close the socket, but do not delete the file
@@ -123,5 +123,5 @@
 
     // listen for connections
-    if (-1 == qListen(listenSocket, 50)) {
+    if (-1 == ::listen(listenSocket, 50)) {
         setError(QLatin1String("QLocalServer::listen"));
         closeServer();
@@ -135,5 +135,5 @@
                                          QSocketNotifier::Read, q);
     q->connect(socketNotifier, SIGNAL(activated(int)),
-               q, SLOT(_q_socketActivated()));
+               q, SLOT(_q_onNewConnection()));
     socketNotifier->setEnabled(maxPendingConnections > 0);
     return true;
@@ -151,7 +151,9 @@
     listenSocket = -1;
 
-    if (socketNotifier)
+    if (socketNotifier) {
+        socketNotifier->setEnabled(false); // Otherwise, closed socket is checked before deleter runs
         socketNotifier->deleteLater();
-    socketNotifier = 0;
+        socketNotifier = 0;
+    }
 
     if (!fullServerName.isEmpty())
@@ -165,5 +167,5 @@
     Accept the new socket.
  */
-void QLocalServerPrivate::_q_socketActivated()
+void QLocalServerPrivate::_q_onNewConnection()
 {
     Q_Q(QLocalServer);
@@ -172,6 +174,6 @@
 
     ::sockaddr_un addr;
-    QT_SOCKLEN_T length = sizeof(sockaddr_un);
-    int connectedSocket = qAccept(listenSocket, (sockaddr *)&addr, &length);
+    int length = sizeof(sockaddr_un);
+    int connectedSocket = ::accept(listenSocket, (sockaddr *)&addr, &length);
     if(-1 == connectedSocket) {
         setError(QLatin1String("QLocalSocket::activated"));
@@ -194,22 +196,12 @@
     timeout.tv_usec = (msec % 1000) * 1000;
 
-    // timeout can not be 0 or else select will return an error.
-    if (0 == msec)
-        timeout.tv_usec = 1000;
-
     int result = -1;
-    // on Linux timeout will be updated by select, but _not_ on other systems.
-    QTime timer;
-    timer.start();
-    while (pendingConnections.isEmpty() && (-1 == msec || timer.elapsed() < msec)) {
-        result = ::select(listenSocket + 1, &readfds, 0, 0, &timeout);
-        if (-1 == result && errno != EINTR) {
-            setError(QLatin1String("QLocalServer::waitForNewConnection"));
-            closeServer();
-            break;
-        }
-        if (result > 0)
-            _q_socketActivated();
-    }
+    result = ::select(listenSocket + 1, &readfds, 0, 0, (msec == -1) ? 0 : &timeout);
+    if (-1 == result) {
+        setError(QLatin1String("QLocalServer::waitForNewConnection"));
+        closeServer();
+    }
+    if (result > 0)
+        _q_onNewConnection();
     if (timedOut)
         *timedOut = (result == 0);
Index: /trunk/src/network/socket/qlocalsocket_os2.cpp
===================================================================
--- /trunk/src/network/socket/qlocalsocket_os2.cpp	(revision 567)
+++ /trunk/src/network/socket/qlocalsocket_os2.cpp	(revision 568)
@@ -235,5 +235,5 @@
 
     // create the socket
-    if (-1 == (d->connectingSocket = qSocket(PF_UNIX, SOCK_STREAM, 0))) {
+    if (-1 == (d->connectingSocket = ::socket(PF_UNIX, SOCK_STREAM, 0))) {
         d->errorOccurred(UnsupportedSocketOperationError,
                         QLatin1String("QLocalSocket::connectToServer"));
@@ -285,5 +285,5 @@
     ::memcpy(name.sun_path, connectingPathName.toLatin1().data(),
              connectingPathName.toLatin1().size() + 1);
-    if (-1 == qConnect(connectingSocket, (struct sockaddr *)&name, sizeof(name))) {
+    if (-1 == ::connect(connectingSocket, (struct sockaddr *)&name, sizeof(name))) {
         QString function = QLatin1String("QLocalSocket::connectToServer");
         switch (errno)
@@ -306,5 +306,5 @@
             // Try again later, all of the sockets listening are full
             if (!delayConnect) {
-                delayConnect = new QSocketNotifier(connectingSocket, QSocketNotifier::Write);
+                delayConnect = new QSocketNotifier(connectingSocket, QSocketNotifier::Write, q);
                 q->connect(delayConnect, SIGNAL(activated(int)), q, SLOT(_q_connectToSocket()));
             }
@@ -325,9 +325,6 @@
 
     // connected!
-    if (delayConnect) {
-        delayConnect->setEnabled(false);
-        delete delayConnect;
-        delayConnect = 0;
-    }
+    cancelDelayedConnect();
+
     serverName = connectingName;
     fullServerName = connectingPathName;
@@ -376,4 +373,16 @@
 }
 
+void QLocalSocketPrivate::cancelDelayedConnect()
+{
+    if (delayConnect) {
+        delayConnect->setEnabled(false);
+        delete delayConnect;
+        delayConnect = 0;
+        connectTimer->stop();
+        delete connectTimer;
+        connectTimer = 0;
+    }
+}
+
 quintptr QLocalSocket::socketDescriptor() const
 {
@@ -422,12 +431,5 @@
     Q_D(QLocalSocket);
     d->unixSocket.close();
-    if (d->delayConnect) {
-        d->delayConnect->setEnabled(false);
-        delete d->delayConnect;
-        d->delayConnect = 0;
-        d->connectTimer->stop();
-        delete d->connectTimer;
-        d->connectTimer = 0;
-    }
+    d->cancelDelayedConnect();
     if (d->connectingSocket != -1)
         ::close(d->connectingSocket);
@@ -515,7 +517,7 @@
         return (state() == ConnectedState);
 
-    fd_set readfds;
-    FD_ZERO(&readfds);
-    FD_SET(d->connectingSocket, &readfds);
+    fd_set fds;
+    FD_ZERO(&fds);
+    FD_SET(d->connectingSocket, &fds);
 
     timeval timeout;
@@ -533,5 +535,5 @@
     while (state() == ConnectingState
            && (-1 == msec || timer.elapsed() < msec)) {
-        result = ::select(d->connectingSocket + 1, &readfds, 0, 0, &timeout);
+        result = ::select(d->connectingSocket + 1, &fds, 0, 0, &timeout);
         if (-1 == result && errno != EINTR) {
             d->errorOccurred( QLocalSocket::UnknownSocketError,
Index: /trunk/src/network/socket/qnativesocketengine_os2.cpp
===================================================================
--- /trunk/src/network/socket/qnativesocketengine_os2.cpp	(revision 567)
+++ /trunk/src/network/socket/qnativesocketengine_os2.cpp	(revision 568)
@@ -53,4 +53,5 @@
 #include <fcntl.h>
 #include <sys/filio.h>
+#include <netinet/tcp.h>
 
 #if defined QNATIVESOCKETENGINE_DEBUG
@@ -109,12 +110,11 @@
     \a port and \a addr if they are non-null.
 */
-static inline void qt_socket_getPortAndAddress(struct sockaddr *sa, quint16 *port, QHostAddress *addr)
-{
-    struct sockaddr_in *sa4 = (struct sockaddr_in *)sa;
+static inline void qt_socket_getPortAndAddress(const qt_sockaddr *s, quint16 *port, QHostAddress *addr)
+{
     if (port)
-        *port = ntohs(sa4->sin_port);
+        *port = ntohs(s->a4.sin_port);
     if (addr) {
         QHostAddress tmpAddress;
-        tmpAddress.setAddress(ntohl(sa4->sin_addr.s_addr));
+        tmpAddress.setAddress(ntohl(s->a4.sin_addr.s_addr));
         *addr = tmpAddress;
     }
@@ -132,5 +132,5 @@
     int protocol = AF_INET;
     int type = (socketType == QAbstractSocket::UdpSocket) ? SOCK_DGRAM : SOCK_STREAM;
-    int socket = qt_socket_socket(protocol, type, 0);
+    int socket = ::socket(protocol, type, 0);
 
     if (socket <= 0) {
@@ -159,4 +159,5 @@
     // Ensure that the socket is closed on exec*().
     ::fcntl(socket, F_SETFD, FD_CLOEXEC);
+
     socketDescriptor = socket;
     return true;
@@ -173,4 +174,6 @@
 
     int n = -1;
+    int level = SOL_SOCKET; // default
+
     switch (opt) {
     case QNativeSocketEngine::ReceiveBufferSocketOption:
@@ -192,10 +195,18 @@
         n = SO_OOBINLINE;
         break;
+    case QNativeSocketEngine::LowDelayOption:
+        level = IPPROTO_TCP;
+        n = TCP_NODELAY;
+        break;
+    case QNativeSocketEngine::KeepAliveOption:
+        n = SO_KEEPALIVE;
+        break;
     }
 
     int v = -1;
-    QT_SOCKOPTLEN_T len = sizeof(v);
-    if (getsockopt(socketDescriptor, SOL_SOCKET, n, (char *) &v, &len) != -1)
+    int len = sizeof(v);
+    if (::getsockopt(socketDescriptor, level, n, (char *) &v, &len) != -1)
         return v;
+
     return -1;
 }
@@ -212,4 +223,6 @@
 
     int n = 0;
+    int level = SOL_SOCKET; // default
+
     switch (opt) {
     case QNativeSocketEngine::ReceiveBufferSocketOption:
@@ -252,14 +265,25 @@
         n = SO_OOBINLINE;
         break;
-    }
-
-    return ::setsockopt(socketDescriptor, SOL_SOCKET, n, (char *) &v, sizeof(v)) == 0;
+    case QNativeSocketEngine::LowDelayOption:
+        level = IPPROTO_TCP;
+        n = TCP_NODELAY;
+        break;
+    case QNativeSocketEngine::KeepAliveOption:
+        n = SO_KEEPALIVE;
+        break;
+    }
+
+    return ::setsockopt(socketDescriptor, level, n, (char *) &v, sizeof(v)) == 0;
 }
 
 bool QNativeSocketEnginePrivate::nativeConnect(const QHostAddress &addr, quint16 port)
 {
+#ifdef QNATIVESOCKETENGINE_DEBUG
+    qDebug("QNativeSocketEnginePrivate::nativeConnect() : %d ", socketDescriptor);
+#endif
+
     struct sockaddr_in sockAddrIPv4;
     struct sockaddr *sockAddrPtr = 0;
-    QT_SOCKLEN_T sockAddrSize = 0;
+    int sockAddrSize = 0;
 
     if (addr.protocol() == QAbstractSocket::IPv4Protocol) {
@@ -275,5 +299,5 @@
     }
 
-    int connectResult = QT_SOCKET_CONNECT(socketDescriptor, sockAddrPtr, sockAddrSize);
+    int connectResult = ::connect(socketDescriptor, sockAddrPtr, sockAddrSize);
     if (connectResult == -1) {
         switch (errno) {
@@ -347,5 +371,5 @@
     struct sockaddr_in sockAddrIPv4;
     struct sockaddr *sockAddrPtr = 0;
-    QT_SOCKLEN_T sockAddrSize = 0;
+    int sockAddrSize = 0;
 
     if (address.protocol() == QAbstractSocket::IPv4Protocol) {
@@ -360,5 +384,6 @@
     }
 
-    int bindResult = QT_SOCKET_BIND(socketDescriptor, sockAddrPtr, sockAddrSize);
+    int bindResult = ::bind(socketDescriptor, sockAddrPtr, sockAddrSize);
+
     if (bindResult < 0) {
         switch(errno) {
@@ -397,5 +422,5 @@
 bool QNativeSocketEnginePrivate::nativeListen(int backlog)
 {
-    if (qt_socket_listen(socketDescriptor, backlog) < 0) {
+    if (::listen(socketDescriptor, backlog) < 0) {
         switch (errno) {
         case EADDRINUSE:
@@ -424,10 +449,13 @@
 int QNativeSocketEnginePrivate::nativeAccept()
 {
-    int acceptedDescriptor = qt_socket_accept(socketDescriptor, 0, 0);
+    int acceptedDescriptor = ::accept(socketDescriptor, 0, 0);
 #if defined (QNATIVESOCKETENGINE_DEBUG)
     qDebug("QNativeSocketEnginePrivate::nativeAccept() == %i", acceptedDescriptor);
 #endif
-    // Ensure that the socket is closed on exec*()
-    ::fcntl(acceptedDescriptor, F_SETFD, FD_CLOEXEC);
+    //check if we have vaild descriptor at all
+    if(acceptedDescriptor > 0) {
+        // Ensure that the socket is closed on exec*()
+        ::fcntl(acceptedDescriptor, F_SETFD, FD_CLOEXEC);
+    }
     return acceptedDescriptor;
 }
@@ -435,24 +463,9 @@
 qint64 QNativeSocketEnginePrivate::nativeBytesAvailable() const
 {
-    /*
-      Apparently, there is not consistency among different operating
-      systems on how to use FIONREAD.
-
-      FreeBSD, Linux and Solaris all expect the 3rd argument to
-      ioctl() to be an int, which is normally 32-bit even on 64-bit
-      machines.
-
-      IRIX, on the other hand, expects a size_t, which is 64-bit on
-      64-bit machines.
-
-      So, the solution is to use size_t initialized to zero to make
-      sure all bits are set to zero, preventing underflow with the
-      FreeBSD/Linux/Solaris ioctls.
-    */
-    size_t nbytes = 0;
+    int nbytes = 0;
     // gives shorter than true amounts on Unix domain sockets.
     qint64 available = 0;
-    if (::ioctl(socketDescriptor, FIONREAD, (char *) &nbytes) >= 0)
-        available = (qint64) *((int *) &nbytes);
+	if (::ioctl(socketDescriptor, FIONREAD, (char *) &nbytes) >= 0)
+        available = (qint64) nbytes;
 
 #if defined (QNATIVESOCKETENGINE_DEBUG)
@@ -465,11 +478,7 @@
 {
     // Create a sockaddr struct and reset its port number.
-    struct sockaddr storage;
-    sockaddr *storagePtr = reinterpret_cast<sockaddr *>(&storage);
-    storagePtr->sa_family = 0;
-
-    sockaddr_in *storagePtrIPv4 = reinterpret_cast<sockaddr_in *>(&storage);
-    storagePtrIPv4->sin_port = 0;
-    QT_SOCKLEN_T storageSize = sizeof(storage);
+    qt_sockaddr storage;
+    int storageSize = sizeof(storage);
+    memset(&storage, 0, storageSize);
 
     // Peek 0 bytes into the next message. The size of the message may
@@ -478,5 +487,5 @@
     do {
         char c;
-        readBytes = ::recvfrom(socketDescriptor, &c, 1, MSG_PEEK, storagePtr, &storageSize);
+        readBytes = ::recvfrom(socketDescriptor, &c, 1, MSG_PEEK, &storage.a, &storageSize);
     } while (readBytes == -1 && errno == EINTR);
 
@@ -496,4 +505,5 @@
     QVarLengthArray<char, 8192> udpMessagePeekBuffer(8192);
     ssize_t recvResult = -1;
+
     for (;;) {
         // the data written to udpMessagePeekBuffer is discarded, so
@@ -521,7 +531,7 @@
                                                     QHostAddress *address, quint16 *port)
 {
-    struct sockaddr_in aa;
+    qt_sockaddr aa;
     memset(&aa, 0, sizeof(aa));
-    QT_SOCKLEN_T sz;
+    int sz;
     sz = sizeof(aa);
 
@@ -530,5 +540,5 @@
         char c;
         recvFromResult = ::recvfrom(socketDescriptor, maxSize ? data : &c, maxSize ? maxSize : 1,
-                                    0, (struct sockaddr *)&aa, &sz);
+                                    0, &aa.a, &sz);
     } while (recvFromResult == -1 && errno == EINTR);
 
@@ -536,5 +546,5 @@
         setError(QAbstractSocket::NetworkError, ReceiveDatagramErrorString);
     } else if (port || address) {
-        qt_socket_getPortAndAddress((struct sockaddr *) &aa, port, address);
+        qt_socket_getPortAndAddress(&aa, port, address);
     }
 
@@ -554,23 +564,19 @@
     struct sockaddr_in sockAddrIPv4;
     struct sockaddr *sockAddrPtr = 0;
-    QT_SOCKLEN_T sockAddrSize = 0;
+    int sockAddrSize = 0;
 
     if (host.protocol() == QAbstractSocket::IPv4Protocol) {
-	memset(&sockAddrIPv4, 0, sizeof(sockAddrIPv4));
-	sockAddrIPv4.sin_family = AF_INET;
-	sockAddrIPv4.sin_port = htons(port);
-	sockAddrIPv4.sin_addr.s_addr = htonl(host.toIPv4Address());
-	sockAddrSize = sizeof(sockAddrIPv4);
-	sockAddrPtr = (struct sockaddr *)&sockAddrIPv4;
+    memset(&sockAddrIPv4, 0, sizeof(sockAddrIPv4));
+    sockAddrIPv4.sin_family = AF_INET;
+    sockAddrIPv4.sin_port = htons(port);
+    sockAddrIPv4.sin_addr.s_addr = htonl(host.toIPv4Address());
+    sockAddrSize = sizeof(sockAddrIPv4);
+    sockAddrPtr = (struct sockaddr *)&sockAddrIPv4;
     }
 
     // ignore the SIGPIPE signal
     qt_ignore_sigpipe();
-
-    ssize_t sentBytes;
-    do {
-        sentBytes = ::sendto(socketDescriptor, data, len,
-                             0, sockAddrPtr, sockAddrSize);
-    } while (sentBytes == -1 && errno == EINTR);
+    ssize_t sentBytes = ::sendto(socketDescriptor, data, len,
+                                       0, sockAddrPtr, sockAddrSize);
 
     if (sentBytes < 0) {
@@ -603,15 +609,14 @@
         return false;
 
-    struct sockaddr_in sa;
-    struct sockaddr *sockAddrPtr = (struct sockaddr *) &sa;
-    QT_SOCKLEN_T sockAddrSize = sizeof(sa);
+    qt_sockaddr sa;
+    int sockAddrSize = sizeof(sa);
 
     // Determine local address
     memset(&sa, 0, sizeof(sa));
-    if (::getsockname(socketDescriptor, sockAddrPtr, &sockAddrSize) == 0) {
-        qt_socket_getPortAndAddress(sockAddrPtr, &localPort, &localAddress);
+    if (::getsockname(socketDescriptor, &sa.a, &sockAddrSize) == 0) {
+        qt_socket_getPortAndAddress(&sa, &localPort, &localAddress);
 
         // Determine protocol family
-        switch (sockAddrPtr->sa_family) {
+        switch (sa.a.sa_family) {
         case AF_INET:
             socketProtocol = QAbstractSocket::IPv4Protocol;
@@ -628,10 +633,10 @@
 
     // Determine the remote address
-    if (!::getpeername(socketDescriptor, sockAddrPtr, &sockAddrSize))
-        qt_socket_getPortAndAddress(sockAddrPtr, &peerPort, &peerAddress);
+    if (!::getpeername(socketDescriptor, &sa.a, &sockAddrSize))
+        qt_socket_getPortAndAddress(&sa, &peerPort, &peerAddress);
 
     // Determine the socket type (UDP/TCP)
     int value = 0;
-    QT_SOCKOPTLEN_T valueSize = sizeof(int);
+    int valueSize = sizeof(int);
     if (::getsockopt(socketDescriptor, SOL_SOCKET, SO_TYPE, &value, &valueSize) == 0) {
         if (value == SOCK_STREAM)
@@ -679,5 +684,5 @@
     ssize_t writtenBytes;
     do {
-        writtenBytes = ::write(socketDescriptor, data, len);
+	    writtenBytes = ::write(socketDescriptor, data, len);
     } while (writtenBytes < 0 && errno == EINTR);
 
@@ -766,30 +771,9 @@
     tv.tv_usec = (timeout % 1000) * 1000;
 
-    QTime timer;
-    timer.start();
-
     int retval;
-    do {
-        if (selectForRead)
-            retval = select(socketDescriptor + 1, &fds, 0, 0, timeout < 0 ? 0 : &tv);
-        else
-            retval = select(socketDescriptor + 1, 0, &fds, 0, timeout < 0 ? 0 : &tv);
-
-        if (retval != -1 || errno != EINTR)
-            break;
-
-        if (timeout > 0) {
-            // recalculate the timeout
-            int t = timeout - timer.elapsed();
-            if (t < 0) {
-                // oops, timeout turned negative?
-                retval = -1;
-                break;
-            }
-
-            tv.tv_sec = t / 1000;
-            tv.tv_usec = (t % 1000) * 1000;
-        }
-    } while (true);
+    if (selectForRead)
+        retval = ::select(socketDescriptor + 1, &fds, 0, 0, timeout < 0 ? 0 : &tv);
+    else
+        retval = ::select(socketDescriptor + 1, 0, &fds, 0, timeout < 0 ? 0 : &tv);
 
     return retval;
@@ -797,5 +781,5 @@
 
 int QNativeSocketEnginePrivate::nativeSelect(int timeout, bool checkRead, bool checkWrite,
-				      bool *selectForRead, bool *selectForWrite) const
+                       bool *selectForRead, bool *selectForWrite) const
 {
     fd_set fdread;
@@ -813,10 +797,11 @@
     tv.tv_usec = (timeout % 1000) * 1000;
 
+    int ret;
     QTime timer;
     timer.start();
 
-    int ret;
     do {
-        ret = select(socketDescriptor + 1, &fdread, &fdwrite, 0, timeout < 0 ? 0 : &tv);
+        ret = ::select(socketDescriptor + 1, &fdread, &fdwrite, 0, timeout < 0 ? 0 : &tv);
+
         if (ret != -1 || errno != EINTR)
             break;
@@ -835,4 +820,5 @@
         }
     } while (true);
+
     if (ret <= 0)
         return ret;
@@ -840,4 +826,5 @@
     *selectForRead = FD_ISSET(socketDescriptor, &fdread);
     *selectForWrite = FD_ISSET(socketDescriptor, &fdwrite);
+
     return ret;
 }
