Index: configure.ac
===================================================================
--- configure.ac	(revision 14343)
+++ configure.ac	(working copy)
@@ -451,7 +451,8 @@
    *mingw32*)
      have_msw="yes"
      CXXFLAGS="$CXXFLAGS -mms-bitfields -mwin32 -mwindows"
-     CPPFLAGS="$CPPFLAGS -DWIN32_LEAN_AND_MEAN"
+     CPPFLAGS="$CPPFLAGS -DWINVER=0x0600 -D_WIN32_WINNT=0x0600 -DWIN32_LEAN_AND_MEAN"
+     CPPFLAGS="$CPPFLAGS -D__USE_MINGW_ANSI_STDIO=1 -D__STDC_FORMAT_MACROS=1"
      LIBS="$LIBS -liphlpapi -lshell32 -lws2_32"
      transmissionlocaledir="locale"
      if test -z "$host_alias"; then
Index: daemon/daemon.c
===================================================================
--- daemon/daemon.c	(revision 14343)
+++ daemon/daemon.c	(working copy)
@@ -11,12 +11,16 @@
 #include <stdio.h> /* printf */
 #include <stdlib.h> /* exit, atoi */
 
-#include <fcntl.h> /* open */
 #include <signal.h>
 #ifdef HAVE_SYSLOG
 #include <syslog.h>
 #endif
-#include <unistd.h> /* daemon */
+#ifndef _WIN32
+ #include <fcntl.h> /* open */
+ #include <unistd.h> /* daemon */
+#else
+ #include <process.h> /* getpid */
+#endif
 
 #include <event2/buffer.h>
 #include <event2/event.h>
@@ -171,6 +175,7 @@
 {
     switch (sig)
     {
+#ifdef SIGHUP
         case SIGHUP:
         {
             if (!mySession)
@@ -200,6 +205,7 @@
             }
             break;
         }
+#endif
 
         default:
             tr_logAddError ("Unexpected signal (%d) in daemon, closing.", sig);
@@ -433,7 +439,7 @@
 
     signal (SIGINT, gotsig);
     signal (SIGTERM, gotsig);
-#ifndef _WIN32
+#ifdef SIGHUP
     signal (SIGHUP, gotsig);
 #endif
 
@@ -615,9 +621,11 @@
 
     mySession = session;
 
+#ifdef SIGHUP
     /* If we got a SIGHUP during startup, process that now. */
     if (seenHUP)
         gotsig (SIGHUP);
+#endif
 
     /* maybe add a watchdir */
     {
@@ -693,7 +701,7 @@
     printf (" done.\n");
 
     /* shutdown */
-#if HAVE_SYSLOG
+#ifdef HAVE_SYSLOG
     if (!foreground)
     {
         syslog (LOG_INFO, "%s", "Closing session");
Index: libtransmission/blocklist-test.c
===================================================================
--- libtransmission/blocklist-test.c	(revision 14343)
+++ libtransmission/blocklist-test.c	(working copy)
@@ -10,8 +10,13 @@
 #include <assert.h>
 #include <stdio.h>
 #include <string.h> /* strlen () */
-#include <unistd.h> /* sync() */
 
+#ifndef _WIN32
+ #include <unistd.h> /* sync() */
+#else
+ #define sync() (void)0
+#endif
+
 #include "transmission.h"
 #include "blocklist.h"
 #include "file.h"
Index: libtransmission/fdlimit.c
===================================================================
--- libtransmission/fdlimit.c	(revision 14343)
+++ libtransmission/fdlimit.c	(working copy)
@@ -12,8 +12,10 @@
 #include <inttypes.h>
 #include <string.h>
 
-#include <sys/time.h> /* getrlimit */
-#include <sys/resource.h> /* getrlimit */
+#ifndef _WIN32
+ #include <sys/time.h> /* getrlimit */
+ #include <sys/resource.h> /* getrlimit */
+#endif
 
 #include "transmission.h"
 #include "error.h"
@@ -310,7 +312,9 @@
 
   if (session->fdInfo == NULL)
     {
+#ifndef _WIN32
       struct rlimit limit;
+#endif
       struct tr_fdInfo * i;
       const int FILE_CACHE_SIZE = 32;
 
@@ -319,6 +323,7 @@
       fileset_construct (&i->fileset, FILE_CACHE_SIZE);
       session->fdInfo = i;
 
+#ifndef _WIN32
       /* set the open-file limit to the largest safe size wrt FD_SETSIZE */
       if (!getrlimit (RLIMIT_NOFILE, &limit))
         {
@@ -332,6 +337,7 @@
               tr_logAddInfo ("Changed open file limit from %d to %d", old_limit, (int)limit.rlim_cur);
             }
         }
+#endif
     }
 }
 
Index: libtransmission/file-test.c
===================================================================
--- libtransmission/file-test.c	(revision 14343)
+++ libtransmission/file-test.c	(working copy)
@@ -9,7 +9,7 @@
 
 #include <string.h>
 
-#ifndef WIN32
+#ifndef _WIN32
  #include <sys/types.h>
  #include <sys/stat.h>
  #include <unistd.h>
@@ -23,7 +23,7 @@
 
 #include "libtransmission-test.h"
 
-#ifndef WIN32
+#ifndef _WIN32
  #define NATIVE_PATH_SEP "/"
 #else
  #define NATIVE_PATH_SEP "\\"
@@ -42,7 +42,7 @@
 static bool
 create_symlink (const char * dst_path, const char * src_path, bool dst_is_dir)
 {
-#ifndef WIN32
+#ifndef _WIN32
 
   (void) dst_is_dir;
 
@@ -71,7 +71,7 @@
 static bool
 create_hardlink (const char * dst_path, const char * src_path)
 {
-#ifndef WIN32
+#ifndef _WIN32
 
   return link (src_path, dst_path) != -1;
 
@@ -109,7 +109,7 @@
       char * pathPart;
       const char * slashPos = strchr (p, '/');
 
-#ifdef WIN32
+#ifdef _WIN32
 
       const char * backslashPos = strchr (p, '\\');
       if (slashPos == NULL || (backslashPos != NULL && backslashPos < slashPos))
@@ -617,7 +617,7 @@
   check_streq (".", name);
   tr_free (name);
 
-#ifdef WIN32
+#ifdef _WIN32
 
   name = tr_sys_path_basename ("c:\\a\\b\\c", &err);
   check (name != NULL);
Index: libtransmission/file.h
===================================================================
--- libtransmission/file.h	(revision 14343)
+++ libtransmission/file.h	(working copy)
@@ -13,7 +13,7 @@
 #include <inttypes.h>
 #include <time.h>
 
-#ifdef WIN32
+#ifdef _WIN32
  #include <windows.h>
 #endif
 
Index: libtransmission/jsonsl.h
===================================================================
--- libtransmission/jsonsl.h	(revision 14343)
+++ libtransmission/jsonsl.h	(working copy)
@@ -39,7 +39,6 @@
 #include <stdlib.h>
 #include <stddef.h>
 #include <string.h>
-#include <sys/types.h>
 #include <wchar.h>
 
 #ifdef __cplusplus
@@ -69,6 +68,7 @@
 typedef int ssize_t;
 #endif
 #else
+#include <sys/types.h>
 #include <stdint.h>
 #endif
 
Index: libtransmission/libtransmission-test.c
===================================================================
--- libtransmission/libtransmission-test.c	(revision 14343)
+++ libtransmission/libtransmission-test.c	(working copy)
@@ -8,11 +8,22 @@
  */
 
 #include <assert.h>
+#include <errno.h>
 #include <stdio.h>
 #include <stdlib.h> /* mkstemp() */
+#include <string.h> /* strcmp() */
 
-#include <unistd.h>
+#ifndef _WIN32
+ #include <unistd.h> /* sync() */
+#else
+ #define sync() (void)0
+#endif
 
+/***
+****
+***/
+
+
 #include "transmission.h"
 #include "error.h"
 #include "file.h"
@@ -19,6 +30,7 @@
 #include "platform.h" /* TR_PATH_DELIMETER */
 #include "torrent.h"
 #include "trevent.h"
+#include "variant.h"
 #include "libtransmission-test.h"
 
 bool verbose = false;
@@ -117,17 +129,6 @@
 ****
 ***/
 
-#include <unistd.h> /* sync() */
-
-#include <errno.h>
-#include <string.h> /* strcmp() */
-
-#include "variant.h"
-
-/***
-****
-***/
-
 static char*
 tr_getcwd (void)
 {
Index: libtransmission/makemeta-test.c
===================================================================
--- libtransmission/makemeta-test.c	(revision 14343)
+++ libtransmission/makemeta-test.c	(working copy)
@@ -14,7 +14,9 @@
 #include "file.h"
 #include "makemeta.h"
 
-#include <unistd.h> /* sync() */
+#ifndef _WIN32
+ #include <unistd.h> /* sync() */
+#endif
 
 #include <stdlib.h> /* mktemp() */
 #include <string.h> /* strlen() */
Index: libtransmission/move-test.c
===================================================================
--- libtransmission/move-test.c	(revision 14343)
+++ libtransmission/move-test.c	(working copy)
@@ -12,7 +12,11 @@
 #include <string.h> /* strcmp() */
 #include <stdio.h>
 
-#include <unistd.h> /* sync() */
+#ifndef _WIN32
+ #include <unistd.h> /* sync() */
+#else
+ #define sync() (void)0
+#endif
 
 #include <event2/buffer.h>
 
Index: libtransmission/net.c
===================================================================
--- libtransmission/net.c	(revision 14343)
+++ libtransmission/net.c	(working copy)
@@ -27,12 +27,11 @@
 #include <string.h>
 #include <assert.h>
 
-#include <sys/types.h>
-
 #ifdef _WIN32
- #define _WIN32_WINNT   0x0501
+ #include <windows.h>
  #include <ws2tcpip.h>
 #else
+ #include <sys/types.h>
  #include <netinet/tcp.h>       /* TCP_CONGESTION */
 #endif
 
Index: libtransmission/platform-quota.c
===================================================================
--- libtransmission/platform-quota.c	(revision 14343)
+++ libtransmission/platform-quota.c	(working copy)
@@ -10,11 +10,11 @@
 #include <errno.h>
 #include <stdlib.h>
 #include <string.h>
-#include <unistd.h> /* getuid() */
 #include <event2/util.h> /* evutil_ascii_strcasecmp () */
 
 #ifndef _WIN32
  #include <sys/types.h> /* types needed by quota.h */
+ #include <unistd.h> /* getuid() */
  #if defined(__FreeBSD__) || defined(__OpenBSD__)
   #include <ufs/ufs/quota.h> /* quotactl() */
  #elif defined (__NetBSD__)
Index: libtransmission/platform.c
===================================================================
--- libtransmission/platform.c	(revision 14343)
+++ libtransmission/platform.c	(working copy)
@@ -15,7 +15,6 @@
 #include <assert.h>
 #include <stdlib.h>
 #include <string.h>
-#include <unistd.h> /* getuid() */
 
 #ifdef __HAIKU__
  #include <limits.h> /* PATH_MAX */
@@ -22,11 +21,10 @@
 #endif
 
 #ifdef _WIN32
- #include <w32api.h>
- #define WINVER  WindowsXP
  #include <windows.h>
  #include <shlobj.h> /* for CSIDL_APPDATA, CSIDL_MYDOCUMENTS */
 #else
+ #include <unistd.h> /* getuid() */
  #ifdef BUILD_MAC_CLIENT
   #include <CoreFoundation/CoreFoundation.h>
  #endif
@@ -36,10 +34,6 @@
  #include <pthread.h>
 #endif
 
-#ifdef _WIN32
-#include <libgen.h> /* dirname() */
-#endif
-
 #include "transmission.h"
 #include "file.h"
 #include "list.h"
@@ -268,7 +262,7 @@
   return home;
 }
 
-#if defined (__APPLE__) || defined (WIN32)
+#if defined (__APPLE__) || defined (_WIN32)
  #define RESUME_SUBDIR  "Resume"
  #define TORRENT_SUBDIR "Torrents"
 #else
Index: libtransmission/port-forwarding.c
===================================================================
--- libtransmission/port-forwarding.c	(revision 14343)
+++ libtransmission/port-forwarding.c	(working copy)
@@ -10,7 +10,9 @@
 #include <assert.h>
 #include <stdio.h>
 
-#include <sys/types.h>
+#ifndef _WIN32
+ #include <sys/types.h>
+#endif
 
 #include <event2/event.h>
 
Index: libtransmission/rename-test.c
===================================================================
--- libtransmission/rename-test.c	(revision 14343)
+++ libtransmission/rename-test.c	(working copy)
@@ -12,7 +12,11 @@
 #include <stdio.h> /* fopen() */
 #include <string.h> /* strcmp() */
 
-#include <unistd.h> /* sync() */
+#ifndef _WIN32
+ #include <unistd.h> /* sync() */
+#else
+ #define sync() (void)0
+#endif
 
 #include "transmission.h"
 #include "file.h"
Index: libtransmission/session.c
===================================================================
--- libtransmission/session.c	(revision 14343)
+++ libtransmission/session.c	(working copy)
@@ -14,9 +14,12 @@
 #include <string.h> /* memcpy */
 
 #include <signal.h>
-#include <sys/types.h> /* umask () */
-#include <sys/stat.h> /* umask () */
 
+#ifndef _WIN32
+ #include <sys/types.h> /* umask () */
+ #include <sys/stat.h> /* umask () */
+#endif
+
 #include <event2/dns.h> /* evdns_base_free () */
 #include <event2/event.h>
 
@@ -771,11 +774,13 @@
   if (tr_variantDictFindInt (settings, TR_KEY_message_level, &i))
     tr_logSetLevel (i);
 
+#ifndef _WIN32
   if (tr_variantDictFindInt (settings, TR_KEY_umask, &i))
     {
       session->umask = (mode_t)i;
       umask (session->umask);
     }
+#endif
 
   /* misc features */
   if (tr_variantDictFindInt (settings, TR_KEY_cache_size_mb, &i))
Index: libtransmission/torrent.c
===================================================================
--- libtransmission/torrent.c	(revision 14343)
+++ libtransmission/torrent.c	(working copy)
@@ -11,10 +11,10 @@
 #include <signal.h> /* signal () */
 #ifndef _WIN32
  #include <sys/wait.h> /* wait () */
+ #include <unistd.h> /* fork (), execvp (), _exit () */
 #else
  #include <process.h>
 #endif
-#include <unistd.h> /* fork (), execvp (), _exit () */
 
 #include <assert.h>
 #include <math.h>
Index: libtransmission/tr-dht.c
===================================================================
--- libtransmission/tr-dht.c	(revision 14343)
+++ libtransmission/tr-dht.c	(working copy)
@@ -31,12 +31,12 @@
 
 /* posix */
 #include <signal.h> /* sig_atomic_t */
-#include <sys/time.h>
 #ifdef _WIN32
   #include <inttypes.h>
-  #define _WIN32_WINNT  0x0501	/* freeaddrinfo (),getaddrinfo (),getnameinfo () */
+  #include <windows.h>
   #include <ws2tcpip.h>
 #else
+  #include <sys/time.h>
   #include <sys/types.h>
   #include <sys/socket.h> /* socket (), bind () */
   #include <netdb.h>
Index: libtransmission/tr-lpd.c
===================================================================
--- libtransmission/tr-lpd.c	(revision 14343)
+++ libtransmission/tr-lpd.c	(working copy)
@@ -27,16 +27,14 @@
 
 /* posix */
 #include <signal.h> /* sig_atomic_t */
-#include <sys/time.h>
-#include <unistd.h> /* close () */
 #include <ctype.h> /* toupper () */
 #ifdef _WIN32
-  #include <w32api.h>
-  #define WINDOWS  WindowsXP  /* freeaddrinfo (),getaddrinfo (),getnameinfo () */
   #include <inttypes.h>
+  #include <windows.h>
   #include <ws2tcpip.h>
   typedef uint16_t in_port_t;			/* all missing */
 #else
+  #include <sys/time.h>
   #include <sys/types.h>
   #include <sys/socket.h> /* socket (), bind () */
   #include <netinet/in.h> /* sockaddr_in */
@@ -359,8 +357,8 @@
     fail:
     {
         const int save = errno;
-        close (lpd_socket);
-        close (lpd_socket2);
+        evutil_closesocket (lpd_socket);
+        evutil_closesocket (lpd_socket2);
         lpd_socket = lpd_socket2 = -1;
         session = NULL;
         tr_logAddNamedDbg ("LPD", "LPD initialisation failed (errno = %d)", save);
Index: libtransmission/tr-udp.c
===================================================================
--- libtransmission/tr-udp.c	(revision 14343)
+++ libtransmission/tr-udp.c	(working copy)
@@ -25,7 +25,9 @@
 #include <string.h> /* memcmp (), memcpy (), memset () */
 #include <stdlib.h> /* malloc (), free () */
 
-#include <unistd.h> /* close () */
+#ifndef _WIN32
+ #include <unistd.h> /* dup2 () */
+#endif
 
 #include <event2/event.h>
 
@@ -165,7 +167,7 @@
         rc = dup2 (s, ss->udp6_socket);
         if (rc < 0)
             goto fail;
-        close (s);
+        tr_netCloseSocket (s);
     }
 
     if (ss->udp6_bound == NULL)
@@ -180,7 +182,7 @@
        set things up so that we try again next time. */
     tr_logAddNamedError ("UDP", "Couldn't rebind IPv6 socket");
     if (s >= 0)
-        close (s);
+        tr_netCloseSocket (s);
     if (ss->udp6_bound) {
         free (ss->udp6_bound);
         ss->udp6_bound = NULL;
@@ -261,7 +263,7 @@
     rc = bind (ss->udp_socket, (struct sockaddr*)&sin, sizeof (sin));
     if (rc < 0) {
         tr_logAddNamedError ("UDP", "Couldn't bind IPv4 socket");
-        close (ss->udp_socket);
+        tr_netCloseSocket (ss->udp_socket);
         ss->udp_socket = -1;
         goto ipv6;
     }
Index: libtransmission/transmission.h
===================================================================
--- libtransmission/transmission.h	(revision 14343)
+++ libtransmission/transmission.h	(working copy)
@@ -29,9 +29,8 @@
 #include <inttypes.h> /* uintN_t */
 #include <time.h> /* time_t */
 
-#ifdef _WIN32
- #define __USE_MINGW_ANSI_STDIO 1
- #define __STDC_FORMAT_MACROS 1
+#ifdef _MSC_VER
+ typedef intptr_t ssize_t;
 #endif
 
 #if !defined (__cplusplus)
@@ -69,7 +68,7 @@
 #endif
 
 #ifndef TR_PRIuSIZE
- #ifdef _MSC_VER
+ #ifdef _WIN32
   #define TR_PRIuSIZE "Iu"
  #else
   #define TR_PRIuSIZE "zu"
@@ -76,7 +75,7 @@
  #endif
 #endif
 
-#if defined (_WIN32) && defined (_MSC_VER)
+#if defined (_MSC_VER) && !defined (__cplusplus)
  #define inline __inline
 #endif
 
Index: libtransmission/trevent.c
===================================================================
--- libtransmission/trevent.c	(revision 14343)
+++ libtransmission/trevent.c	(working copy)
@@ -121,7 +121,9 @@
 #define pipewrite(a,b,c) write (a,b,c)
 #endif
 
-#include <unistd.h> /* read (), write (), pipe () */
+#ifndef _WIN32
+ #include <unistd.h> /* read (), write (), pipe () */
+#endif
 
 #include "transmission.h"
 #include "platform.h" /* tr_lockLock () */
Index: libtransmission/utils-test.c
===================================================================
--- libtransmission/utils-test.c	(revision 14343)
+++ libtransmission/utils-test.c	(working copy)
@@ -406,8 +406,10 @@
   tr_snprintf (buf, sizeof (buf), "%.0f", tr_truncd (3.3333, 0));
   check_streq ("3", buf);
 
+#ifndef _WIN32
   tr_snprintf (buf, sizeof (buf), "%.2f", tr_truncd (nan, 2));
   check (strstr (buf, "nan") != NULL);
+#endif
 
   return 0;
 }
Index: libtransmission/utils.c
===================================================================
--- libtransmission/utils.c	(revision 14343)
+++ libtransmission/utils.c	(working copy)
@@ -31,17 +31,16 @@
 #ifdef HAVE_ICONV_OPEN
  #include <iconv.h>
 #endif
-#include <sys/time.h>
-#include <unistd.h> /* getpagesize () */
 
 #include <event2/buffer.h>
 #include <event2/event.h>
 
 #ifdef _WIN32
- #include <w32api.h>
- #define WINVER WindowsXP /* freeaddrinfo (), getaddrinfo (), getnameinfo () */
  #include <windows.h> /* Sleep (), GetSystemTimeAsFileTime (), GetEnvironmentVariable () */
  #include <shellapi.h> /* CommandLineToArgv () */
+#else
+ #include <sys/time.h>
+ #include <unistd.h> /* getpagesize () */
 #endif
 
 #include "transmission.h"
@@ -1128,7 +1127,7 @@
   return ret;
 }
 
-#ifdef WIN32
+#ifdef _WIN32
 
 char *
 tr_win32_native_to_utf8 (const wchar_t * text,
Index: libtransmission/utils.h
===================================================================
--- libtransmission/utils.h	(revision 14343)
+++ libtransmission/utils.h	(working copy)
@@ -179,7 +179,7 @@
  */
 char* tr_utf8clean (const char * str, int len) TR_GNUC_MALLOC;
 
-#ifdef WIN32
+#ifdef _WIN32
 
 char    * tr_win32_native_to_utf8    (const wchar_t * text,
                                       int             text_size);
Index: libtransmission/variant-test.c
===================================================================
--- libtransmission/variant-test.c	(revision 14343)
+++ libtransmission/variant-test.c	(working copy)
@@ -100,7 +100,7 @@
   size_t len;
 
   /* string len is designed to overflow */
-  n = tr_snprintf ((char*)buf, sizeof (buf), "%zu:boat", (size_t)(SIZE_MAX-2));
+  n = tr_snprintf ((char*)buf, sizeof (buf), "%"TR_PRIuSIZE":boat", (size_t)(SIZE_MAX-2));
   err = tr_bencParseStr (buf, buf+n, &end, &str, &len);
   check_int_eq (EILSEQ, err);
   check_int_eq (0, len);
