diff --git a/configure b/configure
index 8574025f5d6ffe3f9ceb27c04d2008d4b72f4633..43ea1069b212de639d4bd4ae231f8a6006875b10 100755
--- a/configure
+++ b/configure
@@ -7436,6 +7436,7 @@ done
 
 
 
+
 
 
 for ac_header in \
@@ -7551,6 +7552,7 @@ for ac_header in \
 	termios.h \
 	unistd.h \
 	utime.h \
+	winsock2.h \
 	valgrind/memcheck.h \
 	valgrind/valgrind.h
 
@@ -14580,6 +14582,7 @@ fi
 
 
 
+
 for ac_header in ft2build.h \
                          freetype/freetype.h \
                          freetype/ftglyph.h \
@@ -17694,7 +17697,7 @@ case $host_os in
   mingw32*)
     CRTLIBS="-lmsvcrt"
 
-    SOCKETLIBS="-lws2_32"
+    SOCKETLIBS="-L\$(TOPOBJDIR)/dlls/ws2_32 -lws2_32"
 
     ;;
 esac
diff --git a/configure.ac b/configure.ac
index ce90fcacccab849b234e5fa1af55cf666ae2d003..400011ce8489e7da6a469c268def093139e707d0 100644
--- a/configure.ac
+++ b/configure.ac
@@ -351,6 +351,7 @@ AC_CHECK_HEADERS(\
 	termios.h \
 	unistd.h \
 	utime.h \
+	winsock2.h \
 	valgrind/memcheck.h \
 	valgrind/valgrind.h
 )
@@ -1334,7 +1335,7 @@ dnl Mingw needs explicit msvcrt for linking libwine and winsock for wininet
 case $host_os in
   mingw32*)
     AC_SUBST(CRTLIBS,"-lmsvcrt")
-    AC_SUBST(SOCKETLIBS,"-lws2_32")
+    AC_SUBST(SOCKETLIBS,"-L\$(TOPOBJDIR)/dlls/ws2_32 -lws2_32")
     ;;
 esac
 
diff --git a/dlls/Makedll.rules.in b/dlls/Makedll.rules.in
index 965302f63d4f6b606bf89c40816014ea87d2a002..c91696604d79bc7d3f931d1567b8e4bf1a8b6599 100644
--- a/dlls/Makedll.rules.in
+++ b/dlls/Makedll.rules.in
@@ -18,7 +18,7 @@ MAINSPEC    = $(BASEMODULE).spec
 SPEC_DEF    = $(BASEMODULE).def
 WIN16_FILES = $(SPEC_SRCS16:.spec=.spec.o) $(C_SRCS16:.c=.o) $(EXTRA_OBJS16)
 ALL_OBJS    = @WIN16_FILES@ $(OBJS) $(RC_SRCS:.rc=.res)
-ALL_LIBS    = $(EXTRALIBS) $(LIBPORT) $(LDFLAGS) $(LIBS)
+ALL_LIBS    = $(LIBPORT) $(EXTRALIBS) $(LDFLAGS) $(LIBS)
 IMPLIB_OBJS = $(IMPLIB_SRCS:.c=.o)
 IMPORTLIBFILE = $(IMPORTLIB:%=lib%.$(IMPLIBEXT))
 STATICIMPLIB  = $(IMPORTLIBFILE:.def=.def.a)
diff --git a/dlls/winhttp/Makefile.in b/dlls/winhttp/Makefile.in
index 40e8fa971b357b21f6953acd610ca0a2eb1863cb..78d3814ad20dec70e3942a809162dd20dd500461 100644
--- a/dlls/winhttp/Makefile.in
+++ b/dlls/winhttp/Makefile.in
@@ -6,6 +6,7 @@ MODULE    = winhttp.dll
 IMPORTLIB = winhttp
 IMPORTS   = shlwapi kernel32
 DELAYIMPORTS = crypt32
+EXTRALIBS = @SOCKETLIBS@
 
 C_SRCS = \
 	cookie.c \
diff --git a/include/config.h.in b/include/config.h.in
index 790c6ba673ff655564e5e6e1b1c1864629e69731..4aafaedb716b273f0b426778c0816dee0716c0e9 100644
--- a/include/config.h.in
+++ b/include/config.h.in
@@ -960,6 +960,9 @@
 /* Define to 1 if you have the `waitpid' function. */
 #undef HAVE_WAITPID
 
+/* Define to 1 if you have the <winsock2.h> header file. */
+#undef HAVE_WINSOCK2_H
+
 /* Define to 1 if you have the <X11/extensions/shape.h> header file. */
 #undef HAVE_X11_EXTENSIONS_SHAPE_H
 
diff --git a/include/wine/port.h b/include/wine/port.h
index 4080444a5499dc99a3a56d99b2b17190cec4b9b0..e445c08e3ee8c9c1d07ef6e51ceddfc783f6c81c 100644
--- a/include/wine/port.h
+++ b/include/wine/port.h
@@ -305,6 +305,22 @@ int lstat(const char *file_name, struct stat *buf);
 void *memmove(void *dest, const void *src, size_t len);
 #endif /* !defined(HAVE_MEMMOVE) */
 
+#ifndef HAVE_POLL
+struct pollfd
+{
+    int fd;
+    short events;
+    short revents;
+};
+#define POLLIN   0x01
+#define POLLPRI  0x02
+#define POLLOUT  0x04
+#define POLLERR  0x08
+#define POLLHUP  0x10
+#define POLLNVAL 0x20
+int poll( struct pollfd *fds, unsigned int count, int timeout );
+#endif /* HAVE_POLL */
+
 #ifndef HAVE_PREAD
 ssize_t pread( int fd, void *buf, size_t count, off_t offset );
 #endif /* HAVE_PREAD */
diff --git a/libs/port/Makefile.in b/libs/port/Makefile.in
index da0b708d6be4ebb5a5bb464ac4cc6a3a4bee6dd2..c4c0127fe5f2cc388f3d9491f5f71254122b338a 100644
--- a/libs/port/Makefile.in
+++ b/libs/port/Makefile.in
@@ -21,6 +21,7 @@ C_SRCS = \
 	memcpy_unaligned.c \
 	memmove.c \
 	mkstemps.c \
+	poll.c \
 	pread.c \
 	pwrite.c \
 	readlink.c \
diff --git a/libs/port/poll.c b/libs/port/poll.c
new file mode 100644
index 0000000000000000000000000000000000000000..b633a0d90ef74607bfde52aeb52b3d51edae2a40
--- /dev/null
+++ b/libs/port/poll.c
@@ -0,0 +1,77 @@
+/*
+ * poll function
+ *
+ * Copyright 2008 Alexandre Julliard
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
+ */
+
+#include "config.h"
+#include "wine/port.h"
+
+#ifndef HAVE_POLL
+
+#ifdef HAVE_WINSOCK2_H
+#include <winsock2.h>
+#endif
+#ifdef HAVE_SYS_TIME_H
+#include <sys/time.h>
+#endif
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+int poll( struct pollfd *fds, unsigned int count, int timeout )
+{
+    fd_set read_set, write_set, except_set;
+    unsigned int i;
+    int maxfd = -1, ret;
+
+    FD_ZERO( &read_set );
+    FD_ZERO( &write_set );
+    FD_ZERO( &except_set );
+
+    for (i = 0; i < count; i++)
+    {
+        if (fds[i].fd == -1) continue;
+        if (fds[i].events & (POLLIN|POLLPRI)) FD_SET( fds[i].fd, &read_set  );
+        if (fds[i].events & POLLOUT) FD_SET( fds[i].fd, &write_set );
+        FD_SET( fds[i].fd, &except_set );  /* POLLERR etc. are always selected */
+        if (fds[i].fd > maxfd) maxfd = fds[i].fd;
+    }
+    if (timeout != -1)
+    {
+        struct timeval tv;
+        tv.tv_sec = timeout / 1000;
+        tv.tv_usec = timeout % 1000;
+        ret = select( maxfd + 1, &read_set, &write_set, &except_set, &tv );
+    }
+    else ret = select( maxfd + 1, &read_set, &write_set, &except_set, NULL );
+
+    if (ret >= 0)
+    {
+        for (i = 0; i < count; i++)
+        {
+            fds[i].revents = 0;
+            if (fds[i].fd == -1) continue;
+            if (FD_ISSET( fds[i].fd, &read_set )) fds[i].revents |= POLLIN;
+            if (FD_ISSET( fds[i].fd, &write_set )) fds[i].revents |= POLLOUT;
+            if (FD_ISSET( fds[i].fd, &except_set )) fds[i].revents |= POLLERR;
+        }
+    }
+    return ret;
+}
+
+#endif /* HAVE_POLL */