Index: CMakeLists.txt
===================================================================
--- CMakeLists.txt	(revision 14376)
+++ CMakeLists.txt	(working copy)
@@ -188,7 +188,8 @@
 
 if(WIN32)
     tr_add_external_auto_library(EVENT2 event
-        PATCH_COMMAND "${CMAKE_COMMAND}" -E copy "${THIRD_PARTY_DIR}/event2.cmake" "<SOURCE_DIR>/CMakeLists.txt")
+        PATCH_COMMAND "${CMAKE_COMMAND}" -E copy "${THIRD_PARTY_DIR}/event2.cmake" "<SOURCE_DIR>/CMakeLists.txt"
+              COMMAND "${CMAKE_COMMAND}" -E chdir "<SOURCE_DIR>" patch -p1 -i "${THIRD_PARTY_DIR}/event2-01-win32.patch")
 else()
     tr_add_external_auto_library(EVENT2 event
         BUILD_IN_SOURCE 1
@@ -222,12 +223,14 @@
 endif()
 
 tr_add_external_auto_library(DHT dht
-    PATCH_COMMAND "${CMAKE_COMMAND}" -E copy "${THIRD_PARTY_DIR}/dht.cmake" "<SOURCE_DIR>/CMakeLists.txt")
+    PATCH_COMMAND "${CMAKE_COMMAND}" -E copy "${THIRD_PARTY_DIR}/dht.cmake" "<SOURCE_DIR>/CMakeLists.txt"
+          COMMAND "${CMAKE_COMMAND}" -E chdir "<SOURCE_DIR>" patch -p1 -i "${THIRD_PARTY_DIR}/dht-01-win32.patch")
 
 if(ENABLE_UTP)
     tr_add_external_auto_library(UTP utp
         PATCH_COMMAND "${CMAKE_COMMAND}" -E copy "${THIRD_PARTY_DIR}/utp.cmake" "<SOURCE_DIR>/CMakeLists.txt"
-              COMMAND "${CMAKE_COMMAND}" -E copy "${THIRD_PARTY_DIR}/utp_config.h" "<SOURCE_DIR>/utp_config.h")
+              COMMAND "${CMAKE_COMMAND}" -E copy "${THIRD_PARTY_DIR}/utp_config.h" "<SOURCE_DIR>/utp_config.h"
+              COMMAND "${CMAKE_COMMAND}" -E chdir "<SOURCE_DIR>" patch -p1 -i "${THIRD_PARTY_DIR}/utp-01-win32.patch")
 endif()
 
 if(WITH_INOTIFY)
Index: third-party/dht-01-win32.patch
===================================================================
--- third-party/dht-01-win32.patch	(revision 0)
+++ third-party/dht-01-win32.patch	(working copy)
@@ -0,0 +1,77 @@
+diff --git a/dht.c b/dht.c
+index b511188..eef6377 100644
+--- a/dht.c
++++ b/dht.c
+@@ -36,18 +36,23 @@ THE SOFTWARE.
+ #include <errno.h>
+ #include <string.h>
+ #include <stdarg.h>
++
++#ifndef _WIN32
+ #include <unistd.h>
+ #include <fcntl.h>
+ #include <sys/time.h>
+-
+-#ifndef _WIN32
+ #include <arpa/inet.h>
+ #include <sys/types.h>
+ #include <sys/socket.h>
+ #include <netinet/in.h>
+ #else
+-#include <w32api.h>
+-#define WINVER WindowsXP
++#ifndef _WIN32_WINNT
++#define _WIN32_WINNT 0x0501 /* Windows XP */
++#endif
++#ifndef WINVER
++#define WINVER _WIN32_WINNT
++#endif
++#include <windows.h>
+ #include <ws2tcpip.h>
+ #endif
+ 
+@@ -65,7 +70,9 @@ THE SOFTWARE.
+ 
+ #ifdef _WIN32
+ 
++#undef EAFNOSUPPORT
+ #define EAFNOSUPPORT WSAEAFNOSUPPORT
++
+ static int
+ set_nonblocking(int fd, int nonblocking)
+ {
+@@ -83,7 +90,34 @@ random(void)
+ {
+     return rand();
+ }
++
++/* Windows Vista and later already provide the implementation. */
++#if _WIN32_WINNT < 0x0600
+ extern const char *inet_ntop(int, const void *, char *, socklen_t);
++#endif
++
++#ifdef _MSC_VER
++/* There is no snprintf in MSVCRT. */
++#define snprintf _snprintf
++#endif
++
++/* N.B. Does not return real time. */
++static int
++gettimeofday(struct timeval *tv, void *tz)
++{
++    LARGE_INTEGER frequency, counter;
++
++    QueryPerformanceFrequency(&frequency);
++    QueryPerformanceCounter(&counter);
++
++    tv->tv_sec = counter.QuadPart / frequency.QuadPart;
++    tv->tv_usec = ((counter.QuadPart % frequency.QuadPart) * 1000000) /
++        frequency.QuadPart;
++
++    (void)tz;
++
++    return 0;
++}
+ 
+ #else
+ 
Index: third-party/utp-01-win32.patch
===================================================================
--- third-party/event2-01-win32.patch	(revision 0)
+++ third-party/event2-01-win32.patch	(working copy)
@@ -0,0 +1,15 @@
+diff --git a/WIN32-Code/event2/event-config.h b/WIN32-Code/event2/event-config.h
+index da8c23a..dd8113e 100644
+--- a/WIN32-Code/event2/event-config.h
++++ b/WIN32-Code/event2/event-config.h
+@@ -167,7 +167,9 @@
+ /* #undef _EVENT_HAVE_STRSEP */
+ 
+ /* Define to 1 if you have the `strtok_r' function. */
+-/* #undef _EVENT_HAVE_STRTOK_R */
++#if defined(__MINGW32__) || defined(__MINGW64__)
++#define _EVENT_HAVE_STRTOK_R 1
++#endif
+ 
+ /* Define to 1 if you have the `strtoll' function. */
+ /* #define _EVENT_HAVE_STRTOLL 1 */
Index: third-party/utp-01-win32.patch
===================================================================
--- third-party/utp-01-win32.patch	(revision 0)
+++ third-party/utp-01-win32.patch	(working copy)
@@ -0,0 +1,32 @@
+diff --git a/utp.h b/utp.h
+index 0086d4c..53ba300 100644
+--- a/utp.h
++++ b/utp.h
+@@ -4,12 +4,18 @@
+ #include "utypes.h"
+ 
+ #ifdef WIN32
++#ifndef _CRT_SECURE_NO_DEPRECATE
+ #define _CRT_SECURE_NO_DEPRECATE
++#endif
++#ifndef WIN32_LEAN_AND_MEAN
+ #define WIN32_LEAN_AND_MEAN
++#endif
+ #include <windows.h>
+ #include <winsock2.h>
+ #include <ws2tcpip.h>
++#ifdef _MSC_VER
+ #pragma comment(lib,"ws2_32.lib")
++#endif
+ #else
+ #include <stdlib.h>
+ #include <sys/types.h>
+@@ -142,7 +148,7 @@ struct UTPStats {
+ };
+ 
+ // Get stats for UTP socket
+-void UTP_GetStats(struct UTPSocket *socket, UTPStats *stats);
++void UTP_GetStats(struct UTPSocket *socket, struct UTPStats *stats);
+ #endif
+ 
+ // Close the UTP socket.
