Index: tclWinPort.h
===================================================================
RCS file: /cvsroot/tcl/tcl/win/tclWinPort.h,v
retrieving revision 1.29
diff -c -r1.29 tclWinPort.h
*** tclWinPort.h	27 May 2002 10:14:21 -0000	1.29
--- tclWinPort.h	27 May 2002 21:01:06 -0000
***************
*** 69,74 ****
--- 69,82 ----
  #include <windows.h>
  #undef WIN32_LEAN_AND_MEAN
  
+ /*
+  * Ask for the winsock2 function typedefs, only.  wspiapi.h may be
+  * included, 1) isn't used, and 2) can't cope with typedefs only.
+  * This appears to be a bug in wspiapi.h, so make sure it is excluded.
+  */
+ #define _WSPIAPI_H_
+ #define INCL_WINSOCK_API_PROTOTYPES 0
+ #define INCL_WINSOCK_API_TYPEDEFS   1
  #include <winsock2.h>
  
  #ifdef BUILD_tcl

Index: tclWinSock.c
===================================================================
RCS file: /cvsroot/tcl/tcl/win/tclWinSock.c,v
retrieving revision 1.26
diff -c -r1.26 tclWinSock.c
*** tclWinSock.c	24 May 2002 18:57:09 -0000	1.26
--- tclWinSock.c	28 May 2002 00:15:17 -0000
***************
*** 27,90 ****
  TCL_DECLARE_MUTEX(socketMutex)
  
  /*
!  * The following structure contains pointers to all of the WinSock API entry
!  * points used by Tcl.  It is initialized by InitSockets.  Since we
!  * dynamically load Winsock.dll on demand, we must use this function table
!  * to refer to functions in the socket API.
   */
  
  static struct {
!     HINSTANCE hInstance;	/* Handle to WinSock library. */
!     SOCKET (PASCAL FAR *accept)(SOCKET s, struct sockaddr FAR *addr,
! 	    int FAR *addrlen);
!     int (PASCAL FAR *bind)(SOCKET s, const struct sockaddr FAR *addr,
! 	    int namelen);
!     int (PASCAL FAR *closesocket)(SOCKET s);
!     int (PASCAL FAR *connect)(SOCKET s, const struct sockaddr FAR *name,
! 	    int namelen);
!     int (PASCAL FAR *ioctlsocket)(SOCKET s, long cmd, u_long FAR *argp);
!     int (PASCAL FAR *getsockopt)(SOCKET s, int level, int optname,
! 	    char FAR * optval, int FAR *optlen);
!     u_short (PASCAL FAR *htons)(u_short hostshort);
!     unsigned long (PASCAL FAR *inet_addr)(const char FAR * cp);
!     char FAR * (PASCAL FAR *inet_ntoa)(struct in_addr in);
!     int (PASCAL FAR *listen)(SOCKET s, int backlog);
!     u_short (PASCAL FAR *ntohs)(u_short netshort);
!     int (PASCAL FAR *recv)(SOCKET s, char FAR * buf, int len, int flags);
!     int (PASCAL FAR *select)(int nfds, fd_set FAR * readfds,
! 	    fd_set FAR * writefds, fd_set FAR * exceptfds,
! 	    const struct timeval FAR * tiemout);
!     int (PASCAL FAR *send)(SOCKET s, const char FAR * buf, int len, int flags);
!     int (PASCAL FAR *setsockopt)(SOCKET s, int level, int optname,
! 	    const char FAR * optval, int optlen);
!     int (PASCAL FAR *shutdown)(SOCKET s, int how);
!     SOCKET (PASCAL FAR *socket)(int af, int type, int protocol);
!     struct hostent FAR * (PASCAL FAR *gethostbyname)(const char FAR * name);
!     struct hostent FAR * (PASCAL FAR *gethostbyaddr)(const char FAR *addr,
!             int addrlen, int addrtype);
!     int (PASCAL FAR *gethostname)(char FAR * name, int namelen);
!     int (PASCAL FAR *getpeername)(SOCKET sock, struct sockaddr FAR *name,
!             int FAR *namelen);
!     struct servent FAR * (PASCAL FAR *getservbyname)(const char FAR * name,
! 	    const char FAR * proto);
!     int (PASCAL FAR *getsockname)(SOCKET sock, struct sockaddr FAR *name,
!             int FAR *namelen);
!     int (PASCAL FAR *WSAStartup)(WORD wVersionRequired, LPWSADATA lpWSAData);
!     int (PASCAL FAR *WSACleanup)(void);
!     int (PASCAL FAR *WSAGetLastError)(void);
!     int (PASCAL FAR *WSAAsyncSelect)(SOCKET s, HWND hWnd, u_int wMsg,
! 	    long lEvent);
  } winSock;
  
  /*
   * The following defines declare the messages used on socket windows.
   */
  
! #define SOCKET_MESSAGE	WM_USER+1
! #define SOCKET_SELECT	WM_USER+2
! #define SOCKET_TERMINATE WM_USER+3
! #define SELECT          TRUE
! #define UNSELECT	FALSE
  
  /*
   * The following structure is used to store the data associated with
--- 36,90 ----
  TCL_DECLARE_MUTEX(socketMutex)
  
  /*
!  * The following structure contains pointers to all of the WinSock API
!  * entry points used by Tcl.  It is initialized by InitSockets.  Since
!  * we dynamically load the Winsock DLL on demand, we must use this
!  * function table to refer to functions in the winsock API.
   */
  
  static struct {
!     HMODULE		    hModule;	/* Handle to WinSock library. */
! 
!     /* Winsock 1.1 functions */
!     LPFN_ACCEPT		    accept;
!     LPFN_BIND		    bind;
!     LPFN_CLOSESOCKET	    closesocket;
!     LPFN_CONNECT	    connect;
!     LPFN_GETHOSTBYADDR	    gethostbyaddr;
!     LPFN_GETHOSTBYNAME	    gethostbyname;
!     LPFN_GETHOSTNAME	    gethostname;
!     LPFN_GETPEERNAME	    getpeername;
!     LPFN_GETSERVBYNAME	    getservbyname;
!     LPFN_GETSOCKNAME	    getsockname;
!     LPFN_GETSOCKOPT	    getsockopt;
!     LPFN_HTONS		    htons;
!     LPFN_INET_ADDR	    inet_addr;
!     LPFN_INET_NTOA	    inet_ntoa;
!     LPFN_IOCTLSOCKET	    ioctlsocket;
!     LPFN_LISTEN		    listen;
!     LPFN_NTOHS		    ntohs;
!     LPFN_RECV		    recv;
!     LPFN_SELECT		    select;
!     LPFN_SEND		    send;
!     LPFN_SETSOCKOPT	    setsockopt;
!     LPFN_SHUTDOWN	    shutdown;
!     LPFN_SOCKET		    socket;
!     LPFN_WSAASYNCSELECT	    WSAAsyncSelect;
!     LPFN_WSACLEANUP	    WSACleanup;
!     LPFN_WSAGETLASTERROR    WSAGetLastError;
!     LPFN_WSASTARTUP	    WSAStartup;
! 
  } winSock;
  
  /*
   * The following defines declare the messages used on socket windows.
   */
  
! #define SOCKET_MESSAGE	    WM_USER+1
! #define SOCKET_SELECT	    WM_USER+2
! #define SOCKET_TERMINATE    WM_USER+3
! #define SELECT		    TRUE
! #define UNSELECT	    FALSE
  
  /*
   * The following structure is used to store the data associated with
***************
*** 271,362 ****
  	initialized = 1;
  	Tcl_CreateExitHandler(SocketExitHandler, (ClientData) NULL);
  
! 	winSock.hInstance = LoadLibraryA("wsock32.dll");
  
  	/*
  	 * Initialize the function table.
  	 */
  
! 	if (!SocketsEnabled()) {
  	    return;
  	}
      
! 	winSock.accept = (SOCKET (PASCAL FAR *)(SOCKET s,
! 		struct sockaddr FAR *addr, int FAR *addrlen))
! 	    GetProcAddress(winSock.hInstance, "accept");
! 	winSock.bind = (int (PASCAL FAR *)(SOCKET s,
! 		const struct sockaddr FAR *addr, int namelen))
! 	    GetProcAddress(winSock.hInstance, "bind");
! 	winSock.closesocket = (int (PASCAL FAR *)(SOCKET s))
! 	    GetProcAddress(winSock.hInstance, "closesocket");
! 	winSock.connect = (int (PASCAL FAR *)(SOCKET s,
! 		const struct sockaddr FAR *name, int namelen))
! 	    GetProcAddress(winSock.hInstance, "connect");
! 	winSock.ioctlsocket = (int (PASCAL FAR *)(SOCKET s, long cmd,
! 		u_long FAR *argp))
! 	    GetProcAddress(winSock.hInstance, "ioctlsocket");
! 	winSock.getsockopt = (int (PASCAL FAR *)(SOCKET s,
! 		int level, int optname, char FAR * optval, int FAR *optlen))
! 	    GetProcAddress(winSock.hInstance, "getsockopt");
! 	winSock.htons = (u_short (PASCAL FAR *)(u_short hostshort))
! 	    GetProcAddress(winSock.hInstance, "htons");
! 	winSock.inet_addr = (unsigned long (PASCAL FAR *)(const char FAR *cp))
! 	    GetProcAddress(winSock.hInstance, "inet_addr");
! 	winSock.inet_ntoa = (char FAR * (PASCAL FAR *)(struct in_addr in))
! 	    GetProcAddress(winSock.hInstance, "inet_ntoa");
! 	winSock.listen = (int (PASCAL FAR *)(SOCKET s, int backlog))
! 	    GetProcAddress(winSock.hInstance, "listen");
! 	winSock.ntohs = (u_short (PASCAL FAR *)(u_short netshort))
! 	    GetProcAddress(winSock.hInstance, "ntohs");
! 	winSock.recv = (int (PASCAL FAR *)(SOCKET s, char FAR * buf,
! 		int len, int flags)) GetProcAddress(winSock.hInstance, "recv");
! 	winSock.select = (int (PASCAL FAR *)(int nfds, fd_set FAR * readfds,
! 		fd_set FAR * writefds, fd_set FAR * exceptfds,
! 		const struct timeval FAR * tiemout))
! 	    GetProcAddress(winSock.hInstance, "select");
! 	winSock.send = (int (PASCAL FAR *)(SOCKET s, const char FAR * buf,
! 		int len, int flags)) GetProcAddress(winSock.hInstance, "send");
! 	winSock.setsockopt = (int (PASCAL FAR *)(SOCKET s, int level,
! 		int optname, const char FAR * optval, int optlen))
! 	    GetProcAddress(winSock.hInstance, "setsockopt");
! 	winSock.shutdown = (int (PASCAL FAR *)(SOCKET s, int how))
! 	    GetProcAddress(winSock.hInstance, "shutdown");
! 	winSock.socket = (SOCKET (PASCAL FAR *)(int af, int type,
! 		int protocol)) GetProcAddress(winSock.hInstance, "socket");
! 	winSock.gethostbyaddr = (struct hostent FAR * (PASCAL FAR *)
! 		(const char FAR *addr, int addrlen, int addrtype))
! 	    GetProcAddress(winSock.hInstance, "gethostbyaddr");
! 	winSock.gethostbyname = (struct hostent FAR * (PASCAL FAR *)
! 		(const char FAR *name))
! 	    GetProcAddress(winSock.hInstance, "gethostbyname");
! 	winSock.gethostname = (int (PASCAL FAR *)(char FAR * name,
! 		int namelen)) GetProcAddress(winSock.hInstance, "gethostname");
! 	winSock.getpeername = (int (PASCAL FAR *)(SOCKET sock,
! 		struct sockaddr FAR *name, int FAR *namelen))
! 	    GetProcAddress(winSock.hInstance, "getpeername");
! 	winSock.getservbyname = (struct servent FAR * (PASCAL FAR *)
! 		(const char FAR * name, const char FAR * proto))
! 	    GetProcAddress(winSock.hInstance, "getservbyname");
! 	winSock.getsockname = (int (PASCAL FAR *)(SOCKET sock,
! 		struct sockaddr FAR *name, int FAR *namelen))
! 	    GetProcAddress(winSock.hInstance, "getsockname");
! 	winSock.WSAStartup = (int (PASCAL FAR *)(WORD wVersionRequired,
! 		LPWSADATA lpWSAData)) GetProcAddress(winSock.hInstance, "WSAStartup");
! 	winSock.WSACleanup = (int (PASCAL FAR *)(void))
! 	    GetProcAddress(winSock.hInstance, "WSACleanup");
! 	winSock.WSAGetLastError = (int (PASCAL FAR *)(void))
! 	    GetProcAddress(winSock.hInstance, "WSAGetLastError");
! 	winSock.WSAAsyncSelect = (int (PASCAL FAR *)(SOCKET s, HWND hWnd,
! 		u_int wMsg, long lEvent))
! 	    GetProcAddress(winSock.hInstance, "WSAAsyncSelect");
!     
! 	/*
! 	 * Now check that all fields are properly initialized. If not, return
! 	 * zero to indicate that we failed to initialize properly.
  	 */
      
! 	if ((winSock.hInstance == NULL) ||
! 		(winSock.accept == NULL) ||
  		(winSock.bind == NULL) ||
  		(winSock.closesocket == NULL) ||
  		(winSock.connect == NULL) ||
--- 263,346 ----
  	initialized = 1;
  	Tcl_CreateExitHandler(SocketExitHandler, (ClientData) NULL);
  
! 	/* Try the winsock2 library first. */
! 	winSock.hModule = LoadLibraryA("ws2_32.dll");
! 
! 	/* If not, fall back to winsock 1.1 */
! 	if (winSock.hModule == NULL) {
! 	    winSock.hModule = LoadLibraryA("wsock32.dll");
! 	}
  
  	/*
  	 * Initialize the function table.
  	 */
  
! 	if (winSock.hModule == NULL) {
  	    return;
  	}
      
! 	winSock.accept = (LPFN_ACCEPT)
! 		GetProcAddress(winSock.hModule, "accept");
! 	winSock.bind = (LPFN_BIND)
! 		GetProcAddress(winSock.hModule, "bind");
! 	winSock.closesocket = (LPFN_CLOSESOCKET)
! 		GetProcAddress(winSock.hModule, "closesocket");
! 	winSock.connect = (LPFN_CONNECT)
! 		GetProcAddress(winSock.hModule, "connect");
! 	winSock.gethostbyaddr = (LPFN_GETHOSTBYADDR)
! 		GetProcAddress(winSock.hModule, "gethostbyaddr");
! 	winSock.gethostbyname = (LPFN_GETHOSTBYNAME)
! 		GetProcAddress(winSock.hModule, "gethostbyname");
! 	winSock.gethostname = (LPFN_GETHOSTNAME)
! 		GetProcAddress(winSock.hModule, "gethostname");
! 	winSock.getpeername = (LPFN_GETPEERNAME)
! 		GetProcAddress(winSock.hModule, "getpeername");
! 	winSock.getservbyname = (LPFN_GETSERVBYNAME)
! 		GetProcAddress(winSock.hModule, "getservbyname");
! 	winSock.getsockname = (LPFN_GETSOCKNAME)
! 		GetProcAddress(winSock.hModule, "getsockname");
! 	winSock.getsockopt = (LPFN_GETSOCKOPT)
! 		GetProcAddress(winSock.hModule, "getsockopt");
! 	winSock.htons = (LPFN_HTONS)
! 		GetProcAddress(winSock.hModule, "htons");
! 	winSock.inet_addr = (LPFN_INET_ADDR)
! 		GetProcAddress(winSock.hModule, "inet_addr");
! 	winSock.inet_ntoa = (LPFN_INET_NTOA)
! 		GetProcAddress(winSock.hModule, "inet_ntoa");
! 	winSock.ioctlsocket = (LPFN_IOCTLSOCKET)
! 		GetProcAddress(winSock.hModule, "ioctlsocket");
! 	winSock.listen = (LPFN_LISTEN)
! 		GetProcAddress(winSock.hModule, "listen");
! 	winSock.ntohs = (LPFN_NTOHS)
! 		GetProcAddress(winSock.hModule, "ntohs");
! 	winSock.recv = (LPFN_RECV)
! 		GetProcAddress(winSock.hModule, "recv");
! 	winSock.select = (LPFN_SELECT)
! 		GetProcAddress(winSock.hModule, "select");
! 	winSock.send = (LPFN_SEND)
! 		GetProcAddress(winSock.hModule, "send");
! 	winSock.setsockopt = (LPFN_SETSOCKOPT)
! 		GetProcAddress(winSock.hModule, "setsockopt");
! 	winSock.shutdown = (LPFN_SHUTDOWN)
! 		GetProcAddress(winSock.hModule, "shutdown");
! 	winSock.socket = (LPFN_SOCKET)
! 		GetProcAddress(winSock.hModule, "socket");
! 	winSock.WSAAsyncSelect = (LPFN_WSAASYNCSELECT)
! 		GetProcAddress(winSock.hModule, "WSAAsyncSelect");
! 	winSock.WSACleanup = (LPFN_WSACLEANUP)
! 		GetProcAddress(winSock.hModule, "WSACleanup");
! 	winSock.WSAGetLastError = (LPFN_WSAGETLASTERROR)
! 		GetProcAddress(winSock.hModule, "WSAGetLastError");
! 	winSock.WSAStartup = (LPFN_WSASTARTUP)
! 		GetProcAddress(winSock.hModule, "WSAStartup");
!     
! 	/*
! 	 * Now check that all fields are properly initialized. If not,
! 	 * return zero to indicate that we failed to initialize
! 	 * properly.
  	 */
      
! 	if ((winSock.accept == NULL) ||
  		(winSock.bind == NULL) ||
  		(winSock.closesocket == NULL) ||
  		(winSock.connect == NULL) ||
