diff --git a/CMakeLists.txt b/CMakeLists.txt
index 56c4278..6db411c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -17,7 +17,7 @@
 # To check:
 # (From Daniel Stenberg) The cmake build selected to run gcc with -fPIC on my box while the plain configure script did not.
 # (From Daniel Stenberg) The gcc command line use neither -g nor any -O options. As a developer, I also treasure our configure scripts's --enable-debug option that sets a long range of "picky" compiler options.
-cmake_minimum_required(VERSION 2.6.2 FATAL_ERROR)
+cmake_minimum_required(VERSION 2.6 FATAL_ERROR)
 set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMake;${CMAKE_MODULE_PATH}")
 include(Utilities)
 
@@ -397,6 +397,31 @@ if(NOT HAVE_LDAP_H)
   set(CURL_DISABLE_LDAP ON CACHE BOOL "" FORCE)
 endif()
 
+# Attempt to add CURL_PULL_* definitions to curlbuild.h
+if(HAVE_SYS_TYPES_H)
+  SET(CURL_PULL_SYS_TYPES_H 1)
+  mark_as_advanced(CURL_PULL_SYS_TYPES_H)
+endif(HAVE_SYS_TYPES_H)
+
+if(HAVE_SYS_SOCKET_H)
+  SET(CURL_PULL_SYS_SOCKET_H 1)
+  mark_as_advanced(CURL_PULL_SYS_SOCKET_H)
+endif(HAVE_SYS_SOCKET_H)
+
+if(HAVE_STDINT_H)
+  SET(CURL_PULL_STDINT_H 1)
+  mark_as_advanced(CURL_PULL_STDINT_H)
+endif(HAVE_STDINT_H)
+
+if(HAVE_INTTYPES_H)
+  SET(CURL_PULL_INTTYPES_H 1)
+  mark_as_advanced(CURL_PULL_INTTYPES_H)
+endif(HAVE_INTTYPES_H)
+
+# Remove include/curl/curlbuild.h file if exists.
+# Otherwise the build may be broken.
+execute_process(COMMAND "${CMAKE_COMMAND}" -E remove
+    ${CMAKE_SOURCE_DIR}/include/curl/curlbuild.h ERROR_QUIET)
 
 check_type_size(size_t  SIZEOF_SIZE_T)
 check_type_size(ssize_t  SIZEOF_SSIZE_T)
@@ -744,7 +769,7 @@ if(_FILE_OFFSET_BITS)
   set(_FILE_OFFSET_BITS 64)
 endif(_FILE_OFFSET_BITS)
 set(CMAKE_REQUIRED_FLAGS "-D_FILE_OFFSET_BITS=64")
-set(CMAKE_EXTRA_INCLUDE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/curl/curl.h")
+set(CMAKE_EXTRA_INCLUDE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/include/curl/curl.h")
 check_type_size("curl_off_t" SIZEOF_CURL_OFF_T)
 set(CMAKE_EXTRA_INCLUDE_FILES)
 set(CMAKE_REQUIRED_FLAGS)
@@ -784,7 +809,13 @@ if(CMAKE_COMPILER_IS_GNUCC AND APPLE)
   endif(HAVE_C_FLAG_Wno_long_double)
 endif(CMAKE_COMPILER_IS_GNUCC AND APPLE)
 
-if(HAVE_SOCKLEN_T)
+# Note that "socklen_t" is not used for Win32 builds.
+# This is because socklen_t is defined in winsock2.h,
+# but winsock2.h cannot be included. It is not known if
+# the user has already included winsock.h (perhaps indirectly,
+# via windows.h). If it is so, the build will break with
+# some versions of MSVC (MS VS 2008, for instance).
+if(HAVE_SOCKLEN_T AND NOT WIN32)
   set(CURL_TYPEOF_CURL_SOCKLEN_T "socklen_t")
   check_type_size("socklen_t" CURL_SIZEOF_CURL_SOCKLEN_T)
   if(NOT HAVE_CURL_SIZEOF_CURL_SOCKLEN_T)
diff --git a/include/curl/curlbuild.h.cmake b/include/curl/curlbuild.h.cmake
index 3aa772f..cca2540 100644
--- a/include/curl/curlbuild.h.cmake
+++ b/include/curl/curlbuild.h.cmake
@@ -131,22 +131,19 @@
 #  include <inttypes.h>
 #endif
 
+/* Configure process defines this to 1 when it finds out that system    */
+/* header file sys/socket.h must be included by the external interface. */
+#cmakedefine CURL_PULL_SYS_SOCKET_H ${CURL_PULL_SYS_SOCKET_H}
+#ifdef CURL_PULL_SYS_SOCKET_H
+#  include <sys/socket.h>
+#endif
+
 /* The size of `long', as computed by sizeof. */
 #cmakedefine CURL_SIZEOF_LONG ${CURL_SIZEOF_LONG}
 
 /* Integral data type used for curl_socklen_t. */
 #cmakedefine CURL_TYPEOF_CURL_SOCKLEN_T ${CURL_TYPEOF_CURL_SOCKLEN_T}
 
-/* on windows socklen_t is in here */
-#ifdef _WIN32
-#  include <winsock2.h>
-#  include <ws2tcpip.h>
-#endif
-
-#ifdef HAVE_SYS_SOCKET_H
-#  include <sys/socket.h>
-#endif
-
 /* Data type definition of curl_socklen_t. */
 typedef CURL_TYPEOF_CURL_SOCKLEN_T curl_socklen_t;
 
