Support non-blocking connect on MS-Windows (Bug#20207)
Based on ideas from Kim F. Storm <storm@cua.dk>, see
http://lists.gnu.org/archive/html/emacs-devel/2006-12/msg00873.html.
src/w32proc.c (reader_thread): If the FILE_CONNECT flag is set, call
'_sys_wait_connect'. If it returns STATUS_CONNECT_FAILED, exit
the thread with code 2.
(sys_select): Support 'wfds' in addition to 'rfds'. If a
descriptor in 'wfds' has its bit set, but the corresponding
fd_info member doesn't have its FILE_CONNECT flag set, ignore the
descriptor. Otherwise, acknowledge a successful non-blocking
connect by resetting the FILE_CONNECT flag and setting cp->status
to STATUS_READ_ACKNOWLEDGED.
src/w32.h (STATUS_CONNECT_FAILED): New enumeration value.
(struct _child_process): New member 'errcode'.
(FILE_CONNECT): New flag.
(_sys_wait_connect): Add prototype.
src/w32.c (pfn_WSAEnumNetworkEvents): New function pointer.
(init_winsock): Load WSAEnumNetworkEvents from winsock DLL.
(set_errno): Map WSAEWOULDBLOCK and WSAENOTCONN.
(sys_connect): Support non-blocking 'connect' calls by setting the
FILE_CONNECT flag in the fd_info member and returning EINPROGRESS.
(_sys_read_ahead): Add debug message if this function is called
for a descriptor that waits for a non-blocking connect to complete.
(_sys_wait_connect): New function.
(sys_read): Support STATUS_CONNECT_FAILED. Return the error code
recorded by _sys_wait_connect when the non-blocking connect
failed. Don't call WSAGetLastError before a call to set_errno had
a chance to use its value, since WSAGetLastError clears the last
error.