diff --git a/configure b/configure
index f5abb467600f0454b701730b385f3bff3b1dc842..f083a6281bb63a770ee86dca675d2dd37cd9d84e 100755
--- a/configure
+++ b/configure
@@ -4627,53 +4627,67 @@ done
 
 
 for ac_hdr in \
+	a.out.h \
+	a_out.h \
+	arpa/inet.h \
 	arpa/nameser.h \
 	curses.h \
 	elf.h \
 	float.h \
 	libio.h \
+	link.h \
 	linux/cdrom.h \
 	linux/joystick.h \
 	linux/ucdrom.h \
 	ncurses.h \
 	net/if.h \
 	netinet/in.h \
+	netinet/tcp.h \
 	resolv.h \
+	sched.h \
+	socket.h \
 	strings.h \
 	sys/cdio.h \
 	sys/errno.h \
 	sys/file.h \
 	sys/filio.h \
+	sys/ipc.h \
 	sys/lwp.h \
 	sys/mman.h \
 	sys/modem.h \
 	sys/mount.h \
+	sys/msg.h \
 	sys/param.h \
 	sys/signal.h \
+	sys/shm.h \
+	sys/socket.h \
 	sys/sockio.h \
 	sys/statfs.h \
 	sys/strtio.h \
 	sys/syscall.h \
+	sys/wait.h \
+	sys/v86.h \
+	sys/v86intr.h \
 	sys/vfs.h \
+	sys/vm86.h \
 	syscall.h \
 	ucontext.h \
-	unistd.h \
 	wctype.h \
 
 do
 ac_safe=`echo "$ac_hdr" | sed 'y%./+-%__p_%'`
 echo $ac_n "checking for $ac_hdr""... $ac_c" 1>&6
-echo "configure:4667: checking for $ac_hdr" >&5
+echo "configure:4681: checking for $ac_hdr" >&5
 if eval "test \"`echo '$''{'ac_cv_header_$ac_safe'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 4672 "configure"
+#line 4686 "configure"
 #include "confdefs.h"
 #include <$ac_hdr>
 EOF
 ac_try="$ac_cpp conftest.$ac_ext >/dev/null 2>conftest.out"
-{ (eval echo configure:4677: \"$ac_try\") 1>&5; (eval $ac_try) 2>&5; }
+{ (eval echo configure:4691: \"$ac_try\") 1>&5; (eval $ac_try) 2>&5; }
 ac_err=`grep -v '^ *+' conftest.out`
 if test -z "$ac_err"; then
   rm -rf conftest*
@@ -4700,12 +4714,12 @@ fi
 done
 
 echo $ac_n "checking whether stat file-mode macros are broken""... $ac_c" 1>&6
-echo "configure:4704: checking whether stat file-mode macros are broken" >&5
+echo "configure:4718: checking whether stat file-mode macros are broken" >&5
 if eval "test \"`echo '$''{'ac_cv_header_stat_broken'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 4709 "configure"
+#line 4723 "configure"
 #include "confdefs.h"
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -4758,12 +4772,12 @@ fi
 
 
 echo $ac_n "checking for working const""... $ac_c" 1>&6
-echo "configure:4762: checking for working const" >&5
+echo "configure:4776: checking for working const" >&5
 if eval "test \"`echo '$''{'ac_cv_c_const'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 4767 "configure"
+#line 4781 "configure"
 #include "confdefs.h"
 
 int main() {
@@ -4812,7 +4826,7 @@ ccp = (char const *const *) p;
 
 ; return 0; }
 EOF
-if { (eval echo configure:4816: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
+if { (eval echo configure:4830: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
   rm -rf conftest*
   ac_cv_c_const=yes
 else
@@ -4833,21 +4847,21 @@ EOF
 fi
 
 echo $ac_n "checking for inline""... $ac_c" 1>&6
-echo "configure:4837: checking for inline" >&5
+echo "configure:4851: checking for inline" >&5
 if eval "test \"`echo '$''{'ac_cv_c_inline'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   ac_cv_c_inline=no
 for ac_kw in inline __inline__ __inline; do
   cat > conftest.$ac_ext <<EOF
-#line 4844 "configure"
+#line 4858 "configure"
 #include "confdefs.h"
 
 int main() {
 } $ac_kw foo() {
 ; return 0; }
 EOF
-if { (eval echo configure:4851: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
+if { (eval echo configure:4865: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
   rm -rf conftest*
   ac_cv_c_inline=$ac_kw; break
 else
@@ -4873,12 +4887,12 @@ EOF
 esac
 
 echo $ac_n "checking for ANSI C header files""... $ac_c" 1>&6
-echo "configure:4877: checking for ANSI C header files" >&5
+echo "configure:4891: checking for ANSI C header files" >&5
 if eval "test \"`echo '$''{'ac_cv_header_stdc'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 4882 "configure"
+#line 4896 "configure"
 #include "confdefs.h"
 #include <stdlib.h>
 #include <stdarg.h>
@@ -4886,7 +4900,7 @@ else
 #include <float.h>
 EOF
 ac_try="$ac_cpp conftest.$ac_ext >/dev/null 2>conftest.out"
-{ (eval echo configure:4890: \"$ac_try\") 1>&5; (eval $ac_try) 2>&5; }
+{ (eval echo configure:4904: \"$ac_try\") 1>&5; (eval $ac_try) 2>&5; }
 ac_err=`grep -v '^ *+' conftest.out`
 if test -z "$ac_err"; then
   rm -rf conftest*
@@ -4903,7 +4917,7 @@ rm -f conftest*
 if test $ac_cv_header_stdc = yes; then
   # SunOS 4.x string.h does not declare mem*, contrary to ANSI.
 cat > conftest.$ac_ext <<EOF
-#line 4907 "configure"
+#line 4921 "configure"
 #include "confdefs.h"
 #include <string.h>
 EOF
@@ -4921,7 +4935,7 @@ fi
 if test $ac_cv_header_stdc = yes; then
   # ISC 2.0.2 stdlib.h does not declare free, contrary to ANSI.
 cat > conftest.$ac_ext <<EOF
-#line 4925 "configure"
+#line 4939 "configure"
 #include "confdefs.h"
 #include <stdlib.h>
 EOF
@@ -4942,7 +4956,7 @@ if test "$cross_compiling" = yes; then
   :
 else
   cat > conftest.$ac_ext <<EOF
-#line 4946 "configure"
+#line 4960 "configure"
 #include "confdefs.h"
 #include <ctype.h>
 #define ISLOWER(c) ('a' <= (c) && (c) <= 'z')
@@ -4953,7 +4967,7 @@ if (XOR (islower (i), ISLOWER (i)) || toupper (i) != TOUPPER (i)) exit(2);
 exit (0); }
 
 EOF
-if { (eval echo configure:4957: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest && (./conftest; exit) 2>/dev/null
+if { (eval echo configure:4971: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest && (./conftest; exit) 2>/dev/null
 then
   :
 else
@@ -4977,12 +4991,12 @@ EOF
 fi
 
 echo $ac_n "checking for size_t""... $ac_c" 1>&6
-echo "configure:4981: checking for size_t" >&5
+echo "configure:4995: checking for size_t" >&5
 if eval "test \"`echo '$''{'ac_cv_type_size_t'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 4986 "configure"
+#line 5000 "configure"
 #include "confdefs.h"
 #include <sys/types.h>
 #if STDC_HEADERS
@@ -5010,7 +5024,7 @@ EOF
 fi
 
 echo $ac_n "checking size of long long""... $ac_c" 1>&6
-echo "configure:5014: checking size of long long" >&5
+echo "configure:5028: checking size of long long" >&5
 if eval "test \"`echo '$''{'ac_cv_sizeof_long_long'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
@@ -5018,7 +5032,7 @@ else
   ac_cv_sizeof_long_long=0
 else
   cat > conftest.$ac_ext <<EOF
-#line 5022 "configure"
+#line 5036 "configure"
 #include "confdefs.h"
 #include <stdio.h>
 main()
@@ -5029,7 +5043,7 @@ main()
   exit(0);
 }
 EOF
-if { (eval echo configure:5033: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest && (./conftest; exit) 2>/dev/null
+if { (eval echo configure:5047: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest && (./conftest; exit) 2>/dev/null
 then
   ac_cv_sizeof_long_long=`cat conftestval`
 else
@@ -5052,12 +5066,12 @@ EOF
 if test "$ac_cv_header_linux_joystick_h" = "yes"
 then
    echo $ac_n "checking "whether linux/joystick.h uses the Linux 2.2+ API"""... $ac_c" 1>&6
-echo "configure:5056: checking "whether linux/joystick.h uses the Linux 2.2+ API"" >&5
+echo "configure:5070: checking "whether linux/joystick.h uses the Linux 2.2+ API"" >&5
 if eval "test \"`echo '$''{'wine_cv_linux_joystick_22_api'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 5061 "configure"
+#line 5075 "configure"
 #include "confdefs.h"
 
 	#include <sys/ioctl.h>
@@ -5069,7 +5083,7 @@ int main() {
 /*empty*/
 ; return 0; }
 EOF
-if { (eval echo configure:5073: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
+if { (eval echo configure:5087: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
   rm -rf conftest*
   wine_cv_linux_joystick_22_api=yes
 else
@@ -5096,12 +5110,12 @@ fi
 if test "$ac_cv_header_sys_vfs_h" = "yes"
 then
     echo $ac_n "checking "whether sys/vfs.h defines statfs"""... $ac_c" 1>&6
-echo "configure:5100: checking "whether sys/vfs.h defines statfs"" >&5
+echo "configure:5114: checking "whether sys/vfs.h defines statfs"" >&5
 if eval "test \"`echo '$''{'wine_cv_sys_vfs_has_statfs'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 5105 "configure"
+#line 5119 "configure"
 #include "confdefs.h"
 
 	#include <sys/types.h>
@@ -5118,7 +5132,7 @@ int main() {
 	
 ; return 0; }
 EOF
-if { (eval echo configure:5122: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
+if { (eval echo configure:5136: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
   rm -rf conftest*
   wine_cv_sys_vfs_has_statfs=yes
 else
@@ -5145,12 +5159,12 @@ fi
 if test "$ac_cv_header_sys_statfs_h" = "yes"
 then
     echo $ac_n "checking "whether sys/statfs.h defines statfs"""... $ac_c" 1>&6
-echo "configure:5149: checking "whether sys/statfs.h defines statfs"" >&5
+echo "configure:5163: checking "whether sys/statfs.h defines statfs"" >&5
 if eval "test \"`echo '$''{'wine_cv_sys_statfs_has_statfs'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 5154 "configure"
+#line 5168 "configure"
 #include "confdefs.h"
 
 	#include <sys/types.h>
@@ -5165,7 +5179,7 @@ int main() {
 	
 ; return 0; }
 EOF
-if { (eval echo configure:5169: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
+if { (eval echo configure:5183: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
   rm -rf conftest*
   wine_cv_sys_statfs_has_statfs=yes
 else
@@ -5192,12 +5206,12 @@ fi
 if test "$ac_cv_header_sys_mount_h" = "yes"
 then
     echo $ac_n "checking "whether sys/mount.h defines statfs"""... $ac_c" 1>&6
-echo "configure:5196: checking "whether sys/mount.h defines statfs"" >&5
+echo "configure:5210: checking "whether sys/mount.h defines statfs"" >&5
 if eval "test \"`echo '$''{'wine_cv_sys_mount_has_statfs'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 5201 "configure"
+#line 5215 "configure"
 #include "confdefs.h"
 
 	#include <sys/types.h>
@@ -5212,7 +5226,7 @@ int main() {
 	
 ; return 0; }
 EOF
-if { (eval echo configure:5216: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
+if { (eval echo configure:5230: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
   rm -rf conftest*
   wine_cv_sys_mount_has_statfs=yes
 else
@@ -5238,7 +5252,7 @@ fi
 
 
 echo $ac_n "checking "for statfs.f_bfree"""... $ac_c" 1>&6
-echo "configure:5242: checking "for statfs.f_bfree"" >&5
+echo "configure:5256: checking "for statfs.f_bfree"" >&5
 if eval "test \"`echo '$''{'wine_cv_statfs_bfree'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
@@ -5247,7 +5261,7 @@ else
         wine_cv_statfs_bfree=no
     else
     	cat > conftest.$ac_ext <<EOF
-#line 5251 "configure"
+#line 5265 "configure"
 #include "confdefs.h"
 
 	#include <sys/types.h>
@@ -5274,7 +5288,7 @@ int main() {
 	
 ; return 0; }
 EOF
-if { (eval echo configure:5278: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
+if { (eval echo configure:5292: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
   rm -rf conftest*
   wine_cv_statfs_bfree=yes
 else
@@ -5298,7 +5312,7 @@ EOF
 fi
 
 echo $ac_n "checking "for statfs.f_bavail"""... $ac_c" 1>&6
-echo "configure:5302: checking "for statfs.f_bavail"" >&5
+echo "configure:5316: checking "for statfs.f_bavail"" >&5
 if eval "test \"`echo '$''{'wine_cv_statfs_bavail'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
@@ -5307,7 +5321,7 @@ else
         wine_cv_statfs_bavail=no
     else
     	cat > conftest.$ac_ext <<EOF
-#line 5311 "configure"
+#line 5325 "configure"
 #include "confdefs.h"
 
 	#include <sys/types.h>
@@ -5334,7 +5348,7 @@ int main() {
 	
 ; return 0; }
 EOF
-if { (eval echo configure:5338: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
+if { (eval echo configure:5352: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
   rm -rf conftest*
   wine_cv_statfs_bavail=yes
 else
@@ -5359,7 +5373,7 @@ fi
 
 
 echo $ac_n "checking "for working sigaltstack"""... $ac_c" 1>&6
-echo "configure:5363: checking "for working sigaltstack"" >&5
+echo "configure:5377: checking "for working sigaltstack"" >&5
 if eval "test \"`echo '$''{'ac_cv_c_working_sigaltstack'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
@@ -5368,7 +5382,7 @@ else
 
 else
   cat > conftest.$ac_ext <<EOF
-#line 5372 "configure"
+#line 5386 "configure"
 #include "confdefs.h"
 
 	#include <stdio.h>
@@ -5406,7 +5420,7 @@ else
 	}
 	
 EOF
-if { (eval echo configure:5410: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest && (./conftest; exit) 2>/dev/null
+if { (eval echo configure:5424: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest && (./conftest; exit) 2>/dev/null
 then
   ac_cv_c_working_sigaltstack="yes"
 else
@@ -5433,12 +5447,12 @@ fi
 
 
 echo $ac_n "checking "for msg_accrights in struct msghdr"""... $ac_c" 1>&6
-echo "configure:5437: checking "for msg_accrights in struct msghdr"" >&5
+echo "configure:5451: checking "for msg_accrights in struct msghdr"" >&5
 if eval "test \"`echo '$''{'ac_cv_c_msg_accrights'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 5442 "configure"
+#line 5456 "configure"
 #include "confdefs.h"
 #include <sys/types.h>
 #include <sys/socket.h>
@@ -5446,7 +5460,7 @@ int main() {
 struct msghdr hdr; hdr.msg_accrights=0
 ; return 0; }
 EOF
-if { (eval echo configure:5450: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
+if { (eval echo configure:5464: \"$ac_compile\") 1>&5; (eval $ac_compile) 2>&5; }; then
   rm -rf conftest*
   ac_cv_c_msg_accrights="yes"
 else
@@ -5469,12 +5483,12 @@ fi
 
 
 echo $ac_n "checking "whether we need to define __i386__"""... $ac_c" 1>&6
-echo "configure:5473: checking "whether we need to define __i386__"" >&5
+echo "configure:5487: checking "whether we need to define __i386__"" >&5
 if eval "test \"`echo '$''{'ac_cv_cpp_def_i386'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 5478 "configure"
+#line 5492 "configure"
 #include "confdefs.h"
 #if (defined(i386) || defined(__i386)) && !defined(__i386__)
 yes
diff --git a/configure.in b/configure.in
index 7f02946b697fa516e21cda2cf87b998d6578332f..33075bf2aea6bf7310d316d60b25d71dca14792b 100644
--- a/configure.in
+++ b/configure.in
@@ -532,37 +532,51 @@ AC_CHECK_FUNCS(\
 dnl **** Check for header files ****
 
 AC_CHECK_HEADERS(\
+	a.out.h \
+	a_out.h \
+	arpa/inet.h \
 	arpa/nameser.h \
 	curses.h \
 	elf.h \
 	float.h \
 	libio.h \
+	link.h \
 	linux/cdrom.h \
 	linux/joystick.h \
 	linux/ucdrom.h \
 	ncurses.h \
 	net/if.h \
 	netinet/in.h \
+	netinet/tcp.h \
 	resolv.h \
+	sched.h \
+	socket.h \
 	strings.h \
 	sys/cdio.h \
 	sys/errno.h \
 	sys/file.h \
 	sys/filio.h \
+	sys/ipc.h \
 	sys/lwp.h \
 	sys/mman.h \
 	sys/modem.h \
 	sys/mount.h \
+	sys/msg.h \
 	sys/param.h \
 	sys/signal.h \
+	sys/shm.h \
+	sys/socket.h \
 	sys/sockio.h \
 	sys/statfs.h \
 	sys/strtio.h \
 	sys/syscall.h \
+	sys/wait.h \
+	sys/v86.h \
+	sys/v86intr.h \
 	sys/vfs.h \
+	sys/vm86.h \
 	syscall.h \
 	ucontext.h \
-	unistd.h \
 	wctype.h \
 )
 AC_HEADER_STAT()
diff --git a/debugger/stabs.c b/debugger/stabs.c
index 6a11ff370b30f200c93e17fd0bb5cc319b174213..b318a0d2606d4c00ecc1f4a2e9a35048b0ad21f2 100644
--- a/debugger/stabs.c
+++ b/debugger/stabs.c
@@ -34,14 +34,20 @@
 #ifdef HAVE_ELF_H
 # include <elf.h>
 #endif
-#include <link.h>
+#ifdef HAVE_LINK_H
+# include <link.h>
+#endif
 #ifdef HAVE_SYS_MMAN_H
-#include <sys/mman.h>
+# include <sys/mman.h>
 #endif
 #elif defined(__EMX__)
-#include <a_out.h>
+#ifdef HAVE_A_OUT_H
+# include <a_out.h>
+#endif
 #else
-#include <a.out.h>
+#ifdef HAVE_A_OUT_H
+# include <a.out.h>
+#endif
 #endif
 
 #ifndef N_UNDF
diff --git a/dlls/crtdll/crtdll_main.c b/dlls/crtdll/crtdll_main.c
index 359ea7ee497555021f58fff941a924ea9e6e1de4..99efa78bdeaea1ac9a7bb0a03ac260d16cdbeb9b 100644
--- a/dlls/crtdll/crtdll_main.c
+++ b/dlls/crtdll/crtdll_main.c
@@ -26,6 +26,8 @@ AJ 990101:
  * since we need 2 byte wide characters. - Marcus Meissner, 981031
  */
 
+#include "config.h"
+
 #include <errno.h>
 #include <stdlib.h>
 #include <stdarg.h>
diff --git a/dlls/ole32/compobj.c b/dlls/ole32/compobj.c
index 21ce643fa1bb3583bc70b1bce13462894fd574e2..242d30ceb945c44059fbf8cf6ca2b60e7dc5ae09 100644
--- a/dlls/ole32/compobj.c
+++ b/dlls/ole32/compobj.c
@@ -8,9 +8,8 @@
  */
 
 #include "config.h"
-#ifdef HAVE_UNISTD_H
-# include <unistd.h>
-#endif
+
+#include <unistd.h>
 #include <fcntl.h>
 #include <sys/types.h>
 #include <sys/time.h>
@@ -19,7 +18,9 @@
 # include <sys/file.h>
 #endif
 #include <sys/ioctl.h>
-#include <sys/socket.h>
+#ifdef HAVE_SYS_SOCKET_H
+# include <sys/socket.h>
+#endif
 #ifdef HAVE_SYS_SOCKIO_H
 # include <sys/sockio.h>
 #endif
diff --git a/dlls/oleaut32/variant.c b/dlls/oleaut32/variant.c
index b3a81d89657c79d59b715a8097b83664814a43da..034e475920c30b520a14636a522bd2d2c55f1b81 100644
--- a/dlls/oleaut32/variant.c
+++ b/dlls/oleaut32/variant.c
@@ -19,6 +19,8 @@
  *   - The date manipulations do not support date prior to 1900.
  *   - The parsing does not accept has many formats has the Windows implementation.
  */
+
+#include "config.h"
  
 #include "windef.h"
 #include "oleauto.h"
diff --git a/dlls/winaspi/winaspi16.c b/dlls/winaspi/winaspi16.c
index e98c4cdde3127eba449bde116240db1ad6d1d12e..66306b9456fd8653da0ec0a6ba72d256ec8b47d1 100644
--- a/dlls/winaspi/winaspi16.c
+++ b/dlls/winaspi/winaspi16.c
@@ -1,3 +1,5 @@
+#include "config.h"
+
 #include <stdlib.h>
 #include <sys/types.h>
 #include <sys/stat.h>
diff --git a/dlls/wnaspi32/winaspi32.c b/dlls/wnaspi32/winaspi32.c
index 166656ad21d3679ee3a0eaf51f32a99a6e0d84df..5dc77065ad3ef3c78fe72a2030301a543f583476 100644
--- a/dlls/wnaspi32/winaspi32.c
+++ b/dlls/wnaspi32/winaspi32.c
@@ -1,3 +1,5 @@
+#include "config.h"
+
 #include <stdlib.h>
 #include <sys/types.h>
 #include <sys/stat.h>
diff --git a/files/directory.c b/files/directory.c
index 5966115de01da85d3e086abbd43207dc6f003892..353c99aaf2dcedc6dbb7f7d0011bb0fc11152f1c 100644
--- a/files/directory.c
+++ b/files/directory.c
@@ -4,6 +4,8 @@
  * Copyright 1995 Alexandre Julliard
  */
 
+#include "config.h"
+
 #include <ctype.h>
 #include <stdlib.h>
 #include <string.h>
diff --git a/files/dos_fs.c b/files/dos_fs.c
index fc4527ceccad6e78e1db2d6e337b52318d28f1bb..13b65fc39547373035e1556ce16464e6f5c216a2 100644
--- a/files/dos_fs.c
+++ b/files/dos_fs.c
@@ -1599,7 +1599,7 @@ void DOSFS_UnixTimeToFileTime( time_t unix_time, FILETIME *filetime,
        Claus Fischer, fischer@iue.tuwien.ac.at
        */
 
-#if (SIZEOF_LONG_LONG >= 8)
+#if SIZEOF_LONG_LONG >= 8
 #  define USE_LONG_LONG 1
 #else
 #  define USE_LONG_LONG 0
diff --git a/graphics/ddraw.c b/graphics/ddraw.c
index 852649a971c689b53c14f8b6e30461b6a24aab73..41619e68b962d22d91f866671271ccc02e98b0f6 100644
--- a/graphics/ddraw.c
+++ b/graphics/ddraw.c
@@ -22,10 +22,14 @@
 #include "ts_xutil.h"
 
 #ifdef HAVE_LIBXXSHM
-#include <sys/types.h>
-#include <sys/ipc.h>
-#include <sys/shm.h>
-#include "ts_xshm.h"
+# include <sys/types.h>
+# ifdef HAVE_SYS_IPC_H
+#  include <sys/ipc.h>
+# endif
+# ifdef HAVE_SYS_SHM_H
+#  include <sys/shm.h>
+# endif
+# include "ts_xshm.h"
 #endif /* defined(HAVE_LIBXXSHM) */
 
 #ifdef HAVE_LIBXXF86DGA
@@ -40,7 +44,9 @@
 
 #include <unistd.h>
 #include <assert.h>
-#include <sys/signal.h>
+#ifdef HAVE_SYS_SIGNAL_H
+# include <sys/signal.h>
+#endif
 #include <fcntl.h>
 #include <string.h>
 #include <stdlib.h>
diff --git a/graphics/x11drv/dib.c b/graphics/x11drv/dib.c
index 24e71a6b249577c4da5f63f9259ec2905476c20d..dd6b8bf31688a6971bb374f021465befeda99420 100644
--- a/graphics/x11drv/dib.c
+++ b/graphics/x11drv/dib.c
@@ -11,6 +11,16 @@
 #include "ts_xlib.h"
 #include "ts_xutil.h"
 
+#ifdef HAVE_LIBXXSHM
+# include "ts_xshm.h"
+# ifdef HAVE_SYS_SHM_H
+#  include <sys/shm.h>
+# endif
+# ifdef HAVE_SYS_IPC_H
+#  include <sys/ipc.h>
+# endif
+#endif /* defined(HAVE_LIBXXSHM) */
+
 #include "windef.h"
 #include "bitmap.h"
 #include "x11drv.h"
@@ -22,10 +32,6 @@
 #include "global.h"
 #include "xmalloc.h" /* for XCREATEIMAGE macro */
 
-#include "ts_xshm.h"
-#include <sys/shm.h>
-#include <sys/ipc.h>
-
 DECLARE_DEBUG_CHANNEL(bitmap)
 DECLARE_DEBUG_CHANNEL(x11drv)
 
diff --git a/include/config.h.in b/include/config.h.in
index 69d66c5b6ce08b188b07d6f6fe9eccaa7801ef79..d88778d04f7c90bae06a30d0c1ff39fbaeaa35dc 100644
--- a/include/config.h.in
+++ b/include/config.h.in
@@ -217,6 +217,15 @@
 /* Define if you have the <X11/xpm.h> header file.  */
 #undef HAVE_X11_XPM_H
 
+/* Define if you have the <a.out.h> header file.  */
+#undef HAVE_A_OUT_H
+
+/* Define if you have the <a_out.h> header file.  */
+#undef HAVE_A_OUT_H
+
+/* Define if you have the <arpa/inet.h> header file.  */
+#undef HAVE_ARPA_INET_H
+
 /* Define if you have the <arpa/nameser.h> header file.  */
 #undef HAVE_ARPA_NAMESER_H
 
@@ -235,6 +244,9 @@
 /* Define if you have the <libio.h> header file.  */
 #undef HAVE_LIBIO_H
 
+/* Define if you have the <link.h> header file.  */
+#undef HAVE_LINK_H
+
 /* Define if you have the <linux/cdrom.h> header file.  */
 #undef HAVE_LINUX_CDROM_H
 
@@ -256,9 +268,18 @@
 /* Define if you have the <netinet/in.h> header file.  */
 #undef HAVE_NETINET_IN_H
 
+/* Define if you have the <netinet/tcp.h> header file.  */
+#undef HAVE_NETINET_TCP_H
+
 /* Define if you have the <resolv.h> header file.  */
 #undef HAVE_RESOLV_H
 
+/* Define if you have the <sched.h> header file.  */
+#undef HAVE_SCHED_H
+
+/* Define if you have the <socket.h> header file.  */
+#undef HAVE_SOCKET_H
+
 /* Define if you have the <soundcard.h> header file.  */
 #undef HAVE_SOUNDCARD_H
 
@@ -277,6 +298,9 @@
 /* Define if you have the <sys/filio.h> header file.  */
 #undef HAVE_SYS_FILIO_H
 
+/* Define if you have the <sys/ipc.h> header file.  */
+#undef HAVE_SYS_IPC_H
+
 /* Define if you have the <sys/lwp.h> header file.  */
 #undef HAVE_SYS_LWP_H
 
@@ -289,12 +313,21 @@
 /* Define if you have the <sys/mount.h> header file.  */
 #undef HAVE_SYS_MOUNT_H
 
+/* Define if you have the <sys/msg.h> header file.  */
+#undef HAVE_SYS_MSG_H
+
 /* Define if you have the <sys/param.h> header file.  */
 #undef HAVE_SYS_PARAM_H
 
+/* Define if you have the <sys/shm.h> header file.  */
+#undef HAVE_SYS_SHM_H
+
 /* Define if you have the <sys/signal.h> header file.  */
 #undef HAVE_SYS_SIGNAL_H
 
+/* Define if you have the <sys/socket.h> header file.  */
+#undef HAVE_SYS_SOCKET_H
+
 /* Define if you have the <sys/sockio.h> header file.  */
 #undef HAVE_SYS_SOCKIO_H
 
@@ -310,18 +343,27 @@
 /* Define if you have the <sys/syscall.h> header file.  */
 #undef HAVE_SYS_SYSCALL_H
 
+/* Define if you have the <sys/v86.h> header file.  */
+#undef HAVE_SYS_V86_H
+
+/* Define if you have the <sys/v86intr.h> header file.  */
+#undef HAVE_SYS_V86INTR_H
+
 /* Define if you have the <sys/vfs.h> header file.  */
 #undef HAVE_SYS_VFS_H
 
+/* Define if you have the <sys/vm86.h> header file.  */
+#undef HAVE_SYS_VM86_H
+
+/* Define if you have the <sys/wait.h> header file.  */
+#undef HAVE_SYS_WAIT_H
+
 /* Define if you have the <syscall.h> header file.  */
 #undef HAVE_SYSCALL_H
 
 /* Define if you have the <ucontext.h> header file.  */
 #undef HAVE_UCONTEXT_H
 
-/* Define if you have the <unistd.h> header file.  */
-#undef HAVE_UNISTD_H
-
 /* Define if you have the <wctype.h> header file.  */
 #undef HAVE_WCTYPE_H
 
diff --git a/loader/dos/dosmod.c b/loader/dos/dosmod.c
index 1a44923d7078485eef7f8abc84f65629bcbcee4d..f80a8b3a5e66dd844475350ce237b1446c7061cc 100644
--- a/loader/dos/dosmod.c
+++ b/loader/dos/dosmod.c
@@ -6,6 +6,8 @@
 
 #if defined(linux) && defined(__i386__)
 
+#include "config.h"
+
 /* apparently ELF images are usually loaded high anyway */
 #ifndef __ELF__
 /* if not, force dosmod at high addresses */
@@ -20,12 +22,18 @@ asm(".org 0x110000");
 #include <fcntl.h>
 #include <signal.h>
 #include <sys/stat.h>
-#include <sys/mman.h>
-#include <sys/vm86.h>
+#ifdef HAVE_SYS_MMAN_H
+# include <sys/mman.h>
+#endif
+#ifdef HAVE_SYS_VM86_H
+# include <sys/vm86.h>
+#endif
 #include <sys/time.h>
 #include <sys/types.h>
 #include <sys/ptrace.h>
-#include <sys/wait.h>
+#ifdef HAVE_SYS_WAIT_H
+# include <sys/wait.h>
+#endif
 #include "dosmod.h"
 
  /* FIXME: hack because libc vm86 may be the old syscall version */
diff --git a/loader/dos/dosvm.c b/loader/dos/dosvm.c
index 60a002424ee025bab40437e6885f94ae03033fac..4ae0422eff102419a6d6bfee475e0688675d0dd6 100644
--- a/loader/dos/dosvm.c
+++ b/loader/dos/dosvm.c
@@ -6,6 +6,8 @@
  * This code hasn't been completely cleaned up yet.
  */
 
+#include "config.h"
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -34,8 +36,12 @@ DECLARE_DEBUG_CHANNEL(relay)
 
 #ifdef MZ_SUPPORTED
 
-#include <sys/vm86.h>
-#include <sys/mman.h>
+#ifdef HAVE_SYS_VM86_H
+# include <sys/vm86.h>
+#endif
+#ifdef HAVE_SYS_MMAN_H
+# include <sys/mman.h>
+#endif
 
 #define IF_CLR(ctx) EFL_reg(ctx) &= ~VIF_MASK
 #define IF_ENABLED(ctx) (EFL_reg(ctx) & VIF_MASK)
diff --git a/loader/dos/module.c b/loader/dos/module.c
index 4acebc00fdf40396b251dc9a1d82f4f1730a5ff7..df84d265814da88bd94d960a224be637918f5f15 100644
--- a/loader/dos/module.c
+++ b/loader/dos/module.c
@@ -6,6 +6,8 @@
  * This code hasn't been completely cleaned up yet.
  */
 
+#include "config.h"
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -38,7 +40,9 @@ DEFAULT_DEBUG_CHANNEL(module)
 
 #ifdef MZ_SUPPORTED
 
-#include <sys/mman.h>
+#ifdef HAVE_SYS_MMAN_H
+# include <sys/mman.h>
+#endif
 
 /* define this to try mapping through /proc/pid/mem instead of a temp file,
    but Linus doesn't like mmapping /proc/pid/mem, so it doesn't work for me */
diff --git a/loader/pe_image.c b/loader/pe_image.c
index ac17fc70472525350c3f60c460806eb2bda2a629..6eeaa5c9fa83032c0f2b36094d2660d0668410a3 100644
--- a/loader/pe_image.c
+++ b/loader/pe_image.c
@@ -33,6 +33,8 @@
  *   difficult to support... (See mm/filemap.c).
  */
 
+#include "config.h"
+
 #include <errno.h>
 #include <assert.h>
 #include <stdlib.h>
diff --git a/memory/ldt.c b/memory/ldt.c
index aa0ee3b526d0c3d39f8a283c23e4a61d40148bd6..8130feabdbda2822dec36fd25e1e807a90c36013 100644
--- a/memory/ldt.c
+++ b/memory/ldt.c
@@ -5,6 +5,8 @@
  * Copyright 1995 Alexandre Julliard
  */
 
+#include "config.h"
+
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
@@ -16,7 +18,10 @@ DEFAULT_DEBUG_CHANNEL(ldt)
 #ifdef __i386__
 
 #ifdef linux
-#include <sys/syscall.h>
+
+#ifdef HAVE_SYS_SYSCALL_H
+# include <sys/syscall.h>
+#endif
 
 struct modify_ldt_s 
 {
diff --git a/msdos/int21.c b/msdos/int21.c
index 0a2adb32eda8bebe60a5b868e1f5869fe1274ba5..4186a8c4b47a5ab5b3039783efae2a7c0857b049 100644
--- a/msdos/int21.c
+++ b/msdos/int21.c
@@ -2,6 +2,8 @@
  * DOS interrupt 21h handler
  */
 
+#include "config.h"
+
 #include <time.h>
 #include <fcntl.h>
 #include <errno.h>
diff --git a/objects/gdiobj.c b/objects/gdiobj.c
index c48e0509807ab1040d0fd63018ccc2e045aa0317..458293754b0310bf0e4913ef9749739f31187bb4 100644
--- a/objects/gdiobj.c
+++ b/objects/gdiobj.c
@@ -1027,7 +1027,7 @@ INT WINAPI MulDiv(
 	     INT nMultiplier,
 	     INT nDivisor
 ) {
-#if (SIZEOF_LONG_LONG >= 8)
+#if SIZEOF_LONG_LONG >= 8
     long long ret;
 
     if (!nDivisor) return -1;
diff --git a/relay32/snoop.c b/relay32/snoop.c
index 2b7699ed17115eb1eb4a908f4e94f5797e584aa1..9c1a2ebeea513649c15ea89a3997250f70ab9375 100644
--- a/relay32/snoop.c
+++ b/relay32/snoop.c
@@ -4,6 +4,7 @@
  * Copyright 1998 Marcus Meissner
  */
 
+#include "config.h"
 
 #include <assert.h>
 #include <string.h>
diff --git a/scheduler/client.c b/scheduler/client.c
index 055695264b8fa592b780f22ed4d90103c086d257..c2e7dac3575e4885e02640d94a83327eadb75865 100644
--- a/scheduler/client.c
+++ b/scheduler/client.c
@@ -12,7 +12,9 @@
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
-#include <sys/socket.h>
+#ifdef HAVE_SYS_SOCKET_H
+# include <sys/socket.h>
+#endif
 #ifdef HAVE_SYS_MMAN_H
 #include <sys/mman.h>
 #endif
diff --git a/scheduler/thread.c b/scheduler/thread.c
index a53a23cabee02a103c3c871f1660056c2048b72a..7ba3a0c528c4bc8aa1c960da3495c8c4b413aa06 100644
--- a/scheduler/thread.c
+++ b/scheduler/thread.c
@@ -9,7 +9,9 @@
 #include <assert.h>
 #include <fcntl.h>
 #include <sys/types.h>
-#include <sys/socket.h>
+#ifdef HAVE_SYS_SOCKET_H
+# include <sys/socket.h>
+#endif
 #ifdef HAVE_SYS_MMAN_H
 #include <sys/mman.h>
 #endif
diff --git a/server/console.c b/server/console.c
index c5c26eb6e5dc6059943e11781cd9c08cbcd39f7b..40d7cd32ac9a17cce350c24f28144465342fa21c 100644
--- a/server/console.c
+++ b/server/console.c
@@ -7,6 +7,8 @@
  *        the client-side console support.
  */
 
+#include "config.h"
+
 #include <assert.h>
 #include <fcntl.h>
 #include <signal.h>
diff --git a/server/file.c b/server/file.c
index b7e86e7756fdfbb04313e07735befc9ebc958b42..17dd6eee2cf48520a1e50b35740259b85255b5a0 100644
--- a/server/file.c
+++ b/server/file.c
@@ -4,6 +4,8 @@
  * Copyright (C) 1998 Alexandre Julliard
  */
 
+#include "config.h"
+
 #include <assert.h>
 #include <fcntl.h>
 #include <stdio.h>
diff --git a/server/pipe.c b/server/pipe.c
index c99a8afe460965aacbdcaf223eda72ddd4338664..5459f3e094895d1ffb27545e59f5989aacca9846 100644
--- a/server/pipe.c
+++ b/server/pipe.c
@@ -4,6 +4,8 @@
  * Copyright (C) 1998 Alexandre Julliard
  */
 
+#include "config.h"
+
 #include <assert.h>
 #include <fcntl.h>
 #include <string.h>
diff --git a/server/sock.c b/server/sock.c
index 9005d65c5b74addb054cdb0b9bfa7ef1faed769c..f3aa7bb55110731187d23f93335af703cc13f375 100644
--- a/server/sock.c
+++ b/server/sock.c
@@ -7,18 +7,27 @@
  * on the access of the current handle?
  */
 
+#include "config.h"
+
 #include <assert.h>
 #include <fcntl.h>
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 #include <errno.h>
-#include <sys/errno.h>
+#ifdef HAVE_SYS_ERRNO_H
+# include <sys/errno.h>
+#endif
 #include <sys/stat.h>
 #include <sys/time.h>
 #include <sys/types.h>
-#include <sys/socket.h>
+#ifdef HAVE_SYS_SOCKET_H
+# include <sys/socket.h>
+#endif
 #include <sys/ioctl.h>
+#ifdef HAVE_SYS_FILIO_H
+# include <sys/filio.h>
+#endif
 #include <time.h>
 #include <unistd.h>
 
diff --git a/server/socket.c b/server/socket.c
index abf1a1021dba2ef3a52edc00802067a9a072657d..c540aac042c08301f8c348d16728eaa01dc85582 100644
--- a/server/socket.c
+++ b/server/socket.c
@@ -4,6 +4,8 @@
  * Copyright (C) 1998 Alexandre Julliard
  */
 
+#include "config.h"
+
 #include <assert.h>
 #include <errno.h>
 #include <fcntl.h>
@@ -13,7 +15,9 @@
 #include <string.h>
 #include <sys/time.h>
 #include <sys/types.h>
-#include <sys/socket.h>
+#ifdef HAVE_SYS_SOCKET_H
+# include <sys/socket.h>
+#endif
 #include <sys/uio.h>
 #include <unistd.h>
 
diff --git a/tools/build.c b/tools/build.c
index 65e710646bb96d885893003beff42325f6a47acc..6e0206a370945ae50da721386a382a5309fbbe3a 100644
--- a/tools/build.c
+++ b/tools/build.c
@@ -6,6 +6,8 @@
  * Copyright 1999 Ulrich Weigand
  */
 
+#include "config.h"
+
 #include <assert.h>
 #include <stdio.h>
 #include <stdlib.h>
diff --git a/tools/fnt2bdf.c b/tools/fnt2bdf.c
index 991d76ab26e72c17dcd900ee6418e7d342895b7e..0f1691994d5cb479590ae190cd53d04f5243b46a 100644
--- a/tools/fnt2bdf.c
+++ b/tools/fnt2bdf.c
@@ -7,7 +7,11 @@
  *
  */
 
-#include <sys/param.h>
+#include "config.h"
+
+#ifdef HAVE_SYS_PARAM_H
+# include <sys/param.h>
+#endif
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <stdlib.h>
diff --git a/win32/console.c b/win32/console.c
index 1661a9477ef4802d8d1859544d47fe2ba00226b1..bf04381a1467ddd4a2dbb3eda167a490a9d85470 100644
--- a/win32/console.c
+++ b/win32/console.c
@@ -19,6 +19,8 @@
  * -  LYNX/W32. Works mostly, some keys crash it.
  */
 
+#include "config.h"
+
 #include <stdlib.h>
 #include <unistd.h>
 #include <termios.h>
diff --git a/win32/device.c b/win32/device.c
index 3294e25bedf910a403a28ed5a8dbb089975b1d9f..78e17812226186d096abf498fb404c770d1c9f55 100644
--- a/win32/device.c
+++ b/win32/device.c
@@ -7,6 +7,8 @@
  *
  */
 
+#include "config.h"
+
 #include <errno.h>
 #include <assert.h>
 #include <stdlib.h>
diff --git a/win32/file.c b/win32/file.c
index 67142bd46045a41b532c322aaff9128cab0c802b..ac55d014f5615262f50f88a492f1c254d6781fca 100644
--- a/win32/file.c
+++ b/win32/file.c
@@ -4,6 +4,8 @@
  * Copyright 1995 Martin von Loewis, Sven Verdoolaege, and Cameron Heide
  */
 
+#include "config.h"
+
 #include <errno.h>
 #ifdef HAVE_SYS_ERRNO_H
 #include <sys/errno.h>
diff --git a/win32/process.c b/win32/process.c
index 1036a89a04423e7a5850566a34ccb6bdfba62345..320447d5064c5f28cf67fa70592da5f1e7e47344 100644
--- a/win32/process.c
+++ b/win32/process.c
@@ -38,7 +38,7 @@ void Process_ClockTimeToFileTime( clock_t unix_time, LPFILETIME filetime )
 {
     double td = (unix_time*10000000.0)/CLK_TCK;
     /* Yes, double, because long int might overflow here. */
-#if (SIZEOF_LONG_LONG >= 8)
+#if SIZEOF_LONG_LONG >= 8
     unsigned long long t = td;
     filetime->dwLowDateTime  = (UINT) t;
     filetime->dwHighDateTime = (UINT) (t >> 32);
diff --git a/windows/dinput.c b/windows/dinput.c
index 0b2c0cd52a80af44f70f31b3ce743523df7f44c9..277381d1eccac015b64b93f7c0e168fb490e1d48 100644
--- a/windows/dinput.c
+++ b/windows/dinput.c
@@ -30,7 +30,9 @@
 #include <sys/fcntl.h>
 #include <sys/ioctl.h>
 #include <errno.h>
-#include <sys/errno.h>
+#ifdef HAVE_SYS_ERRNO_H
+# include <sys/errno.h>
+#endif
 #ifdef HAVE_LINUX_JOYSTICK_H
 # include <linux/joystick.h>
 # define JOYDEV	"/dev/js0"