int PhysicalSocket::DoConnect(const SocketAddress& connect_addr)
 {
     if ((s_ == INVALID_SOCKET) && !Create(connect_addr.family(), SOCK_STREAM))
     {
         return SOCKET_ERROR;
     }
     
     sockaddr_storage addr_storage;
     size_t len = connect_addr.ToSockAddrStorage(&addr_storage);
     sockaddr* addr = reinterpret_cast<sockaddr*>(&addr_storage);
     
     int err = ::connect(s_, addr, static_cast<int>(len));
     UpdateLastError();
     if (err == 0)
     {
         state_ = CS_CONNECTED;
     }
     else if (IsBlockingError(GetError()))
     {
         state_ = CS_CONNECTING;
         enabled_events_ |= DE_CONNECT;
     }
     else
     {
         return SOCKET_ERROR;
     }
     
     enabled_events_ |= DE_READ | DE_WRITE;
     
     return 0;
 }
    int PhysicalSocket::Bind(const SocketAddress& bind_addr)
    {
        sockaddr_storage addr_storage;
        size_t len = bind_addr.ToSockAddrStorage(&addr_storage);
        sockaddr* addr = reinterpret_cast<sockaddr*>(&addr_storage);
        int err = ::bind(s_, addr, static_cast<int>(len));
        if (err < 0)
        {
            UpdateLastError();
        }

        return err;
    }
 int PhysicalSocket::SendTo(const void* buffer, size_t length, const SocketAddress& addr)
 {
     sockaddr_storage saddr;
     size_t len = addr.ToSockAddrStorage(&saddr);
     int sent = (int)::sendto(s_, static_cast<const char *>(buffer), static_cast<int>(length),
                         0,
                         reinterpret_cast<sockaddr*>(&saddr), static_cast<int>(len));
     UpdateLastError();
     MaybeRemapSendError();
     if ((sent < 0) && IsBlockingError(GetError()))
     {
         enabled_events_ |= DE_WRITE;
     }
     
     return sent;
 }