#include &lt;sys/types.h&gt;
#include &lt;sys/event.h&gt;
#include &lt;sys/socket.h&gt;
#include &lt;sys/time.h&gt;
#include &lt;netinet/in.h&gt;
#include &lt;arpa/inet.h&gt;
#include &lt;netdb.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;stdbool.h&gt;
#include &lt;unistd.h&gt;
#include &lt;string.h&gt;
#include &lt;errno.h&gt;
#include &lt;fcntl.h&gt;
#include &lt;syslog.h&gt;
#include &lt;libgen.h&gt;
#include &lt;launch.h&gt;
#include &lt;os/log.h&gt;
 

int main(void)
{
    struct sockaddr_storage ss;
    socklen_t slen = sizeof(ss);
    struct kevent kev_init;
    struct kevent kev_listener;
    launch_data_t sockets_dict;
    launch_data_t checkin_response;
    launch_data_t checkin_request;
    //launch_data_t listening_fd_array;
    int *listening_fd_array;
    size_t launchd_fds_count;	// launch_activate_socket
    int err;			// launch_activate_socket

    size_t i;
    int kq;
    int retval = EXIT_SUCCESS;
 
    /*
     * Create a new kernel event queue
     * that we&#39;ll use for our notification.
     *
         * Note the use of the &#39;%m&#39; formatting character.
         * ASL will replace %m with the error string associated 
         * with the current value of errno.
         */

    if (-1 == (kq = kqueue())) {
        os_log_error(OS_LOG_DEFAULT, &#34;kqueue(): %m&#34;);
        retval = EXIT_FAILURE;
        goto done;
    }

    /*
     * Launchd Check-in - ask for out socket
     */

    err = launch_activate_socket(&#34;MyListenerSocket&#34;, &amp;listening_fd_array, &amp;launchd_fds_count);
    if (err != 0) { 
        os_log_error(OS_LOG_DEFAULT, &#34;launch_activate_socket() failed error %d (%s)&#34;, err, strerror(err)); 
    }
    else
        os_log_error(OS_LOG_DEFAULT, &#34;launch_activate_socket() succeeded Sockets %zu&#34;, launchd_fds_count); 

    if (NULL == listening_fd_array) {
        os_log_error(OS_LOG_DEFAULT, &#34;No known sockets found to answer requests on!&#34;);
        retval = EXIT_FAILURE;
        goto done;
    }

    /*
     * Initialize a new kernel event.  This will trigger when
     * a connection occurs on our listener socket.
     */

    for (i = 0; i &lt; launchd_fds_count; i++) {
        int *this_listening_fd = &amp;listening_fd_array[i];
        EV_SET(&amp;kev_init, *this_listening_fd, EVFILT_READ, EV_ADD, 0, 0, NULL);

        if (kevent(kq, &amp;kev_init, 1, NULL, 0, NULL) == -1) {
            os_log_error(OS_LOG_DEFAULT, &#34;kevent(): %m&#34;);
            retval = EXIT_FAILURE;
            goto done;
        }
    }

    for (;;) {

        FILE *the_stream;
        int filedesc, gai_r;
        char nodename[1024];
        char servname[1024];

        /*
         * Get the next event from the kernel event queue.
         */

        if ((filedesc = kevent(kq, NULL, 0, &amp;kev_listener, 1, NULL)) == -1) {
            os_log_error(OS_LOG_DEFAULT, &#34;kevent(): %m&#34;);
            retval = EXIT_FAILURE;
            goto done;

        } else if (filedesc == 0) {
            retval = EXIT_SUCCESS;
            goto done;
        }

        /*
         * Accept an incoming connection.
         */

        if ((filedesc = accept(kev_listener.ident, (struct sockaddr *)&amp;ss, &amp;slen)) == -1) {
            os_log_error(OS_LOG_DEFAULT, &#34;accept(): %m&#34;);
            continue; /* this isn&#39;t fatal */
        }
 
        /*
         * Extract the client&#39;s host and port names so we 
         * can print them out.
         */

        gai_r = getnameinfo((struct sockaddr *)&amp;ss, slen, nodename, sizeof(nodename),
                servname, sizeof(servname), NI_NUMERICSERV | NI_NUMERICHOST);

        if (0 != gai_r) {
            /* Error occured - log it */
            os_log_error(OS_LOG_DEFAULT, &#34;getnameinfo(): %s&#34;, gai_strerror(gai_r));

        } else {

            /*
             * Send our log message off to the syslogd server.  
             */

            os_log_with_type(OS_LOG_DEFAULT, OS_LOG_TYPE_DEFAULT, &#34;Connection established (IP: %{public}s ; Port: %{public}s)&#34;, nodename, servname);
        }

        /*
         * Open the stream and message the client. 
         */

        the_stream = fdopen(filedesc, &#34;r+&#34;);
        os_log_with_type(OS_LOG_DEFAULT, OS_LOG_TYPE_DEFAULT, &#34;got file descriptor %d&#34;, filedesc);

        if (the_stream) {
            fprintf(the_stream, &#34;Hello C World!\n&#34;);
            fclose(the_stream);

        } else {
            close(filedesc);
        }
    }
done:
    return retval;
}
