diff --git a/CMakeLists.txt b/CMakeLists.txt
index 65d4007ccde18241bdb5e0d99da42b280f98e305..24421d40a2ba7fb83c588039f02b4be80c49dc0c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -2,7 +2,22 @@ cmake_minimum_required (VERSION 2.6)
 
 project (pkcs11 C)
 
+set(PKCS11_PROXY_SRCS gck-rpc-module.c gck-rpc-message.c gck-rpc-util.c egg-buffer.c)
+set(PKCS11_DAEMON_SRCS egg-buffer.c gck-rpc-daemon-standalone.c gck-rpc-dispatch.c gck-rpc-message.c gck-rpc-util.c)
+
 add_definitions(-Wall)
-add_library(pkcs11-proxy.so SHARED gck-rpc-module.c gck-rpc-message.c egg-buffer.c)
-add_executable (pkcs11-daemon egg-buffer.c gck-rpc-daemon-standalone.c gck-rpc-dispatch.c gck-rpc-message.c gck-rpc-util.c)
+add_library(pkcs11-proxy SHARED ${PKCS11_PROXY_SRCS})
+add_executable (pkcs11-daemon ${PKCS11_DAEMON_SRCS})
+
+if (WIN32)
+  include_directories("ext/include")
+
+  add_library(dl STATIC IMPORTED)
+  set_property(TARGET dl PROPERTY
+               IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/ext/lib/libdl.a)
+  target_link_libraries (pkcs11-daemon ws2_32)
+  target_link_libraries (pkcs11-proxy ws2_32)
+endif ()
+
+target_link_libraries (pkcs11-proxy pthread)
 target_link_libraries (pkcs11-daemon dl pthread)
diff --git a/config.h b/config.h
index deb959417191edd0de037cf95f993738fe64a0b2..35be6f961fa86cb6c3ae54ce75a1391a4529ac16 100644
--- a/config.h
+++ b/config.h
@@ -6,11 +6,12 @@
 # include <stdint.h>
 # include <stdlib.h>
 # include <limits.h>
+# include <winsock2.h>
 
 typedef uint32_t __uid32_t;
 typedef uint32_t __gid32_t;
 typedef uint32_t uid_t;
-typedef size_t socklen_t;
+typedef int socklen_t;
 
 struct sockaddr_un {
 	uint16_t sun_family;
@@ -23,6 +24,18 @@ enum  {
 	SHUT_RDWR /* No more receptions or transmissions.  */
 };
 
+#ifdef  __MINGW32__
+static inline int inet_aton(const char * cp, struct in_addr *pin)
+{
+        int rc = inet_addr(cp);
+        if (rc == -1 && strcmp(cp, "255.255.255.255"))
+                return 0;
+
+        pin->s_addr = rc;
+        return 1;
+}
+#endif
+
 #endif
 
 #endif	/* CONFIG_H */
diff --git a/ext/include/dlfcn.h b/ext/include/dlfcn.h
new file mode 100644
index 0000000000000000000000000000000000000000..dfc255118b9830e16064fbb54969cde28a167974
--- /dev/null
+++ b/ext/include/dlfcn.h
@@ -0,0 +1,45 @@
+/*
+ * dlfcn-win32
+ * Copyright (c) 2007 Ramiro Polla
+ *
+ * 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 Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#ifndef DLFCN_H
+#define DLFCN_H
+
+/* POSIX says these are implementation-defined.
+ * To simplify use with Windows API, we treat them the same way.
+ */
+
+#define RTLD_LAZY   0
+#define RTLD_NOW    0
+
+#define RTLD_GLOBAL (1 << 1)
+#define RTLD_LOCAL  (1 << 2)
+
+/* These two were added in The Open Group Base Specifications Issue 6.
+ * Note: All other RTLD_* flags in any dlfcn.h are not standard compliant.
+ */
+
+#define RTLD_DEFAULT    0
+#define RTLD_NEXT       0
+
+void *dlopen ( const char *file, int mode );
+int   dlclose( void *handle );
+void *dlsym  ( void *handle, const char *name );
+char *dlerror( void );
+
+#endif /* DLFCN_H */
diff --git a/ext/lib/libdl.a b/ext/lib/libdl.a
new file mode 100644
index 0000000000000000000000000000000000000000..24371c2d76496c10690224c9606a0c799c993dc0
Binary files /dev/null and b/ext/lib/libdl.a differ
diff --git a/gck-rpc-dispatch.c b/gck-rpc-dispatch.c
index dc2ff7642e1b6381615a502440697d1deac24d0e..7dab038d4684576873358ccf260e626bf5a9e707 100644
--- a/gck-rpc-dispatch.c
+++ b/gck-rpc-dispatch.c
@@ -2006,7 +2006,7 @@ static int read_all(int sock, unsigned char *data, size_t len)
 
 	while (len > 0) {
 
-		r = recv(sock, data, len, 0);
+                r = recv(sock, (void *)data, len, 0);
 
 		if (r == 0) {
 			/* Connection was closed on client */
@@ -2035,7 +2035,7 @@ static int write_all(int sock, unsigned char *data, size_t len)
 
 	while (len > 0) {
 
-		r = send(sock, data, len, 0);
+                r = send(sock, (void *)data, len, 0);
 
 		if (r == -1) {
 			if (errno == EPIPE) {
@@ -2204,18 +2204,6 @@ void gck_rpc_layer_accept(void)
 	pkcs11_dispatchers = ds;
 }
 
-#ifdef  __MINGW32__
-int inet_aton(const char * cp, struct in_addr *pin)
-{
-        int rc = inet_addr(cp);
-        if (rc == -1 && strcmp(cp, "255.255.255.255"))
-                return 0;
-
-        pin->s_addr = rc;
-        return 1;
-}
-#endif
-
 int gck_rpc_layer_initialize(const char *prefix, CK_FUNCTION_LIST_PTR module)
 {
 	struct sockaddr_un addr;
@@ -2281,7 +2269,7 @@ int gck_rpc_layer_initialize(const char *prefix, CK_FUNCTION_LIST_PTR module)
                 }
 
 		if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
-			       &one, sizeof(one)) == -1) {
+                               (char *)&one, sizeof(one)) == -1) {
 			gck_rpc_warn
 			    ("couldn't create set pkcs11 socket options : %s",
 			     strerror(errno));
diff --git a/gck-rpc-module.c b/gck-rpc-module.c
index c4d07511ee897a22bc26a431bb75a084a198ea36..228dc017298869d705d2c6d9aa18825b8bccc62d 100644
--- a/gck-rpc-module.c
+++ b/gck-rpc-module.c
@@ -30,11 +30,15 @@
 
 #include <sys/types.h>
 #include <sys/param.h>
-#include <sys/socket.h>
-#include <sys/un.h>
+#ifdef __MINGW32__
+# include <winsock2.h>
+#else
+# include <sys/socket.h>
+# include <sys/un.h>
 #include <arpa/inet.h>
 #include <netinet/in.h>
 #include <netinet/tcp.h>
+#endif
 
 #include <stdlib.h>
 #include <limits.h>
@@ -280,12 +284,14 @@ static CK_RV call_connect(CallState * cs)
 		}
 	}
 
-	/* close on exec */
+#ifndef __MINGW32__
+        /* close on exec */
 	if (fcntl(sock, F_SETFD, 1) == -1) {
 		close(sock);
 		warning(("couldn't secure socket: %s", strerror(errno)));
 		return CKR_DEVICE_ERROR;
 	}
+#endif
 
 	if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
 		close(sock);