// Create the socket.
sock = socket (PF_INET, sock_style, 0);
if (sock == INVALID_SOCKET) {
// Create the socket.
sock = socket (PF_INET, sock_style, 0);
if (sock == INVALID_SOCKET) {
name.sin_port = htons(port); // set port to zero to let system pick
name.sin_addr.s_addr = htonl (INADDR_ANY);
if (bind (sock, (struct sockaddr *) &name, sizeof (name)) != 0) {
name.sin_port = htons(port); // set port to zero to let system pick
name.sin_addr.s_addr = htonl (INADDR_ANY);
if (bind (sock, (struct sockaddr *) &name, sizeof (name)) != 0) {
// Find the assigned port number
length = sizeof(struct sockaddr_in);
if ( getsockname(sock, (struct sockaddr *) &name, &length) ) {
// Find the assigned port number
length = sizeof(struct sockaddr_in);
if ( getsockname(sock, (struct sockaddr *) &name, &length) ) {
// Create the socket.
sock = socket (PF_INET, sock_style, 0);
if (sock == INVALID_SOCKET) {
// Create the socket.
sock = socket (PF_INET, sock_style, 0);
if (sock == INVALID_SOCKET) {
// pick any available port.
sock = make_server_socket();
if ( sock == INVALID_SOCKET ) {
// pick any available port.
sock = make_server_socket();
if ( sock == INVALID_SOCKET ) {
// Blocking TCP
// Specify the maximum length of the connection queue
listen( sock, SG_MAX_SOCKET_QUEUE );
} else {
// Blocking TCP
// Specify the maximum length of the connection queue
listen( sock, SG_MAX_SOCKET_QUEUE );
} else {
// could cause problems so if you see connections
// dropping for unexplained reasons, LOOK HERE!
if ( result == 0 && save_len == 0 && first_read == true ) {
// could cause problems so if you see connections
// dropping for unexplained reasons, LOOK HERE!
if ( result == 0 && save_len == 0 && first_read == true ) {
bool error_condition = false;
if ( writesocket(sock, buf, length) < 0 ) {
bool error_condition = false;
if ( writesocket(sock, buf, length) < 0 ) {
if ( FD_ISSET(sock, &ready) ) {
int msgsock = accept(sock, 0, 0);
if ( msgsock < 0 ) {
if ( FD_ISSET(sock, &ready) ) {
int msgsock = accept(sock, 0, 0);
if ( msgsock < 0 ) {
client_connections.size() );
for ( int i = 0; i < (int)client_connections.size(); ++i ) {
int msgsock = client_connections[i];
client_connections.size() );
for ( int i = 0; i < (int)client_connections.size(); ++i ) {
int msgsock = client_connections[i];
// write the interesting data to the socket
if ( writesocket(msgsock, buf, length) == SOCKET_ERROR ) {
// write the interesting data to the socket
if ( writesocket(msgsock, buf, length) == SOCKET_ERROR ) {
u_long arg = 1;
if (ioctlsocket( sock, FIONBIO, &arg ) != 0) {
int error_code = WSAGetLastError();
u_long arg = 1;
if (ioctlsocket( sock, FIONBIO, &arg ) != 0) {
int error_code = WSAGetLastError();