#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define FAILURE -1
#define SUCCESS 0

// prints the operating system version information
static void print_host_details() {
    // print OS version
    char os_prod_version[100];
    size_t len = sizeof(os_prod_version);
    int ret = sysctlbyname(&#34;kern.osproductversion&#34;, os_prod_version, &amp;len, NULL, 0);
    if (ret != 0) {
        perror(&#34;failed to get MacOS version&#34;);
    } else {
        fprintf(stderr, &#34;MacOS version: %s\n&#34;, os_prod_version);
    }

    // print the kernel info
    struct utsname kernel_info;
    ret = uname(&amp;kernel_info);
    if (ret != 0) {
        perror(&#34;failed to get kernel info&#34;);
    } else {
        fprintf(stderr, &#34;Kernel version: %s\n&#34;, kernel_info.version);
    }
}

// creates a socket bound to loopback address listen()ing for connections.
// upon successful listen(), the server address will be set in bound_addr.
static int create_listening_socket(const int backlog, struct sockaddr_in* bound_addr) {
	// create a socket
	const int server_sock_fd = socket(PF_INET, SOCK_STREAM, 0);
    if (server_sock_fd == -1) {
        fprintf(stderr, &#34;socket creation failed, reason=%s&#34;, strerror(errno));
        return -1;
    }

	int ret = -1;
	struct sockaddr_in bind_addr;
    bind_addr.sin_port = 0; // ephemeral port
    bind_addr.sin_family = AF_INET;
    inet_pton(AF_INET, &#34;127.0.0.1&#34;, &amp;(bind_addr.sin_addr.s_addr)); // loopback address
    // bind the socket
    const int bound = bind(server_sock_fd, (const struct sockaddr *) &amp;bind_addr, sizeof(struct sockaddr_in));
    if (bound != 0) {
        fprintf(stderr, &#34;failed to bind socket, reason=%s\n&#34;, strerror(errno));
        ret = -1;
        goto close;
    }
	// determine the bound address (which includes the newly allocated port)
    socklen_t sock_len = sizeof(struct sockaddr_in);
    if (getsockname(server_sock_fd, (struct sockaddr *) bound_addr, &amp;sock_len) != 0) {
        fprintf(stderr, &#34;failed to determine bound address of socket, fd=%d, reason=%s\n&#34;, server_sock_fd, strerror(errno));
        ret = -1;
        goto close;
    }
	if (listen(server_sock_fd, backlog) != 0) {
        fprintf(stderr, &#34;failed to listen on socket, reason=%s\n&#34;, strerror(errno));
        ret = -1;
        goto close;
    }
    return server_sock_fd; // socket is successfully listening    

close:
	close(server_sock_fd); // clean up on failure

	return ret;
}

/*
 * Creates a socket with a small backlog, which listen()s for connections but doesn&#39;t accept() it.
 * Any connect() attempts will be accumulated into the pending connection queue till the backlog limit is reached.
 * Failures from connect() (which are expected) are then printed out to verify that the error code matches the
 * specification of listen() which says:
 *   The backlog parameter defines the maximum length for the queue of pending connections.  If a connection request
 *	 arrives with the queue full, the client may receive an error with an indication of ECONNREFUSED.
 */
int main() {
	fprintf(stderr, &#34;-----------------------\n&#34;);
	print_host_details();
	fprintf(stderr, &#34;-----------------------\n&#34;);

	int server_fd = -1;
	int socket_fd = -1;
	int ret = FAILURE;

	// create a socket, with a small backlog, which listen()s for connections but doesn&#39;t accept() it.
	// any connect() attempts will be accumulated into the pending connection queue till the backlog limit is reached.
	struct sockaddr_in bound_addr;	
	const int backlog = 1;
	server_fd = create_listening_socket(backlog, &amp;bound_addr);
	if (server_fd &lt; 0) {
		fprintf(stderr, &#34;failed to create listening socket\n&#34;);
		ret = FAILURE;
		goto done;
	}
	const char *server_host = inet_ntoa(bound_addr.sin_addr);
    fprintf(stderr, &#34;started server at at %s:%d with a backlog of %d\n&#34;, server_host, htons(bound_addr.sin_port), backlog);
    
    // do some connect() attempts that will cause pending connections to exceed the backlog limit
    for (int i = 1; i &lt;= backlog + 10; i++) {
		socket_fd = socket(PF_INET, SOCK_STREAM, 0);
		if (socket_fd &lt; 0) {
			fprintf(stderr, &#34;failed to create socket, reason=%s\n&#34;, strerror(errno));
			ret = FAILURE;
			goto done;
		}
		// connect()
		const int c = connect(socket_fd, (const struct sockaddr *) &amp;bound_addr, sizeof(struct sockaddr_in));
		if (c &lt; 0) {
			const int err_code = errno;
			if (err_code != ETIMEDOUT &amp;&amp; err_code != ECONNREFUSED) {
				fprintf(stderr, &#34;connect() attempt %d - failed to connect()&#34; 
					&#34; - %s &lt;-- FAILURE: Unexpected error code, expected either&#34;
					&#34; ETIMEDOUT or ECONNREFUSED but got %d\n&#34;, i, strerror(err_code), err_code);
			} else {
				// got the expected error code
				fprintf(stderr, &#34;connect() attempt %d - failed to connect() - %s\n&#34;, i, strerror(err_code));
			}
		} else {
			fprintf(stderr, &#34;connect() attempt %d - connected\n&#34;, i);
		}
		close(socket_fd);
		socket_fd = -1;
	}
	ret = SUCCESS;

	done:
		if (server_fd != -1) {
			close(server_fd);
		}
		if (socket_fd != -1) {
			close(socket_fd);
		}

	return ret;
}