+#include <iostream>
+#include "platform_api.h"
+#include <msp/core/systemerror.h>
+#include <msp/io/handle_private.h>
+#include "sockaddr_private.h"
+#include "socket.h"
+#include "socket_private.h"
+
+namespace {
+
+class WinSockHelper
+{
+public:
+ WinSockHelper()
+ {
+ WSADATA wsa_data;
+ int err = WSAStartup(0x0002, &wsa_data);
+ if(err)
+ std::cerr<<"Failed to initialize WinSock: "<<err<<'\n';
+ }
+
+ ~WinSockHelper()
+ {
+ WSACleanup();
+ }
+};
+
+WinSockHelper wsh;
+
+}
+
+
+namespace Msp {
+namespace Net {
+
+void Socket::platform_init()
+{
+ *priv->event = CreateEvent(0, false, false, 0);
+}
+
+void Socket::platform_cleanup()
+{
+ closesocket(priv->handle);
+ CloseHandle(*priv->event);
+}
+
+void Socket::set_timeout(const Time::TimeDelta &timeout)
+{
+ DWORD msecs = static_cast<DWORD>(timeout/Time::msec);
+ priv->set_option(SOL_SOCKET, SO_RCVTIMEO, &msecs, sizeof(DWORD));
+ priv->set_option(SOL_SOCKET, SO_SNDTIMEO, &msecs, sizeof(DWORD));
+}
+
+void Socket::set_platform_events(unsigned e)
+{
+ long ws_events = 0;
+ if(e&S_INPUT)
+ ws_events |= FD_READ|FD_CLOSE;
+ if(e&S_CONNECT)
+ ws_events |= FD_CONNECT;
+ if(e&S_ACCEPT)
+ ws_events |= FD_ACCEPT;
+ WSAEventSelect(priv->handle, *priv->event, ws_events);
+}
+
+
+void Socket::Private::set_block(bool b)
+{
+ u_long flag = !b;
+ ioctlsocket(handle, FIONBIO, &flag);
+}
+
+int Socket::Private::set_option(int level, int optname, const void *optval, socklen_t optlen)
+{
+ return setsockopt(handle, level, optname, reinterpret_cast<const char *>(optval), optlen);
+}
+
+int Socket::Private::get_option(int level, int optname, void *optval, socklen_t *optlen)
+{
+ return getsockopt(handle, level, optname, reinterpret_cast<char *>(optval), optlen);
+}
+
+
+unsigned check_sys_error(int ret, const char *func)
+{
+ if(ret<0)
+ {
+ int err_code = WSAGetLastError();
+ if(err_code==WSAEWOULDBLOCK)
+ return 0;
+ else
+ throw system_error(func, err_code);
+ }
+
+ return ret;
+}
+
+bool check_sys_connect_error(int ret)
+{
+ if(ret==SOCKET_ERROR)
+ {
+ int err_code = WSAGetLastError();
+ if(err_code==WSAEWOULDBLOCK)
+ return false;
+ else
+ throw system_error("connect", err_code);
+ }
+
+ return true;
+}
+
+} // namespace Net
+} // namespace Msp