diff --git a/config/ac_func_accept_argtypes.m4 b/config/ac_func_accept_argtypes.m4
index 4ac5ce0..43e9595 100644
--- a/config/ac_func_accept_argtypes.m4
+++ b/config/ac_func_accept_argtypes.m4
@@ -45,8 +45,8 @@ AC_DEFUN([AC_FUNC_ACCEPT_ARGTYPES],
  [AC_CACHE_VAL(ac_cv_func_accept_arg1,dnl
   [AC_CACHE_VAL(ac_cv_func_accept_arg2,dnl
    [AC_CACHE_VAL(ac_cv_func_accept_arg3,dnl
-    [for ac_cv_func_accept_return in 'int' 'unsigned int PASCAL'; do
-      for ac_cv_func_accept_arg1 in 'int' 'unsigned int'; do
+    [for ac_cv_func_accept_return in 'int' 'unsigned int PASCAL' '__int64'; do
+      for ac_cv_func_accept_arg1 in 'int' 'unsigned int' '__int64'; do
        for ac_cv_func_accept_arg2 in 'struct sockaddr *' 'const struct sockaddr *' 'void *'; do
         for ac_cv_func_accept_arg3 in 'int' 'size_t' 'socklen_t' 'unsigned int' 'void'; do
          AC_TRY_COMPILE(
diff --git a/configure b/configure
index f96c0ff..96a1037 100755
--- a/configure
+++ b/configure
@@ -18472,8 +18472,8 @@ else
      if test "${ac_cv_func_accept_arg3+set}" = set; then
   $as_echo_n "(cached) " >&6
 else
-      for ac_cv_func_accept_return in 'int' 'unsigned int PASCAL'; do
-      for ac_cv_func_accept_arg1 in 'int' 'unsigned int'; do
+      for ac_cv_func_accept_return in 'int' 'unsigned int PASCAL' '__int64'; do
+      for ac_cv_func_accept_arg1 in 'int' 'unsigned int' '__int64'; do
        for ac_cv_func_accept_arg2 in 'struct sockaddr *' 'const struct sockaddr *' 'void *'; do
         for ac_cv_func_accept_arg3 in 'int' 'size_t' 'socklen_t' 'unsigned int' 'void'; do
          cat >conftest.$ac_ext <<_ACEOF
diff --git a/src/include/c.h b/src/include/c.h
index bec1a8c..1f2813c 100644
--- a/src/include/c.h
+++ b/src/include/c.h
@@ -58,7 +58,7 @@
 #endif
 #include "postgres_ext.h"
 
-#if _MSC_VER >= 1400
+#if _MSC_VER >= 1400 || defined(WIN64)
 #define errcode __msvc_errcode
 #include <crtdefs.h>
 #undef errcode
diff --git a/src/include/port.h b/src/include/port.h
index 584a89e..c9f61b3 100644
--- a/src/include/port.h
+++ b/src/include/port.h
@@ -324,8 +324,12 @@ extern FILE *pgwin32_fopen(const char *, const char *);
 #define		fopen(a,b) pgwin32_fopen(a,b)
 #endif
 
+#ifndef popen
 #define popen(a,b) _popen(a,b)
+#endif
+#ifndef pclose
 #define pclose(a) _pclose(a)
+#endif
 
 /* New versions of MingW have gettimeofday, old mingw and msvc don't */
 #ifndef HAVE_GETTIMEOFDAY
diff --git a/src/include/port/win32.h b/src/include/port/win32.h
index 1473d9e..9f4ad35 100644
--- a/src/include/port/win32.h
+++ b/src/include/port/win32.h
@@ -4,7 +4,9 @@
 #define WIN32_ONLY_COMPILER
 #endif
 
+#ifndef _WIN32_WINNT
 #define _WIN32_WINNT 0x0501
+#endif
 /*
  * Always build with SSPI support. Keep it as a #define in case
  * we want a switch to disable it sometime in the future.
@@ -18,8 +20,8 @@
 
 #undef ERROR
 #define _WINSOCKAPI_
-#include <windows.h>
 #include <winsock2.h>
+#include <windows.h>
 #include <ws2tcpip.h>
 #undef small
 #include <process.h>
diff --git a/src/include/port/win32/sys/socket.h b/src/include/port/win32/sys/socket.h
index 97a5041..e811919 100644
--- a/src/include/port/win32/sys/socket.h
+++ b/src/include/port/win32/sys/socket.h
@@ -13,6 +13,7 @@
  */
 #include <winsock2.h>
 #include <ws2tcpip.h>
+#include <windows.h>
 
 #undef ERROR
 #undef small
diff --git a/src/port/getaddrinfo.c b/src/port/getaddrinfo.c
index 4133aed..ba2706d 100644
--- a/src/port/getaddrinfo.c
+++ b/src/port/getaddrinfo.c
@@ -329,7 +329,7 @@ gai_strerror(int errcode)
 			return "Not enough memory";
 #endif
 #ifdef EAI_NODATA
-#ifndef WIN32_ONLY_COMPILER		/* MSVC complains because another case has the
+#ifndef WIN32/* MSVC complains because another case has the
 								 * same value */
 		case EAI_NODATA:
 			return "No host data of that type was found";
