Code : 

```
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
// #include <thread>
#include <arpa/inet.h>
#include <iostream>

using namespace std;

#define PORT 8888
#define BACKLOG 5

int main(int argc, char const *argv[]) {

    int status;
    struct addrinfo hints,
                    *res,
                    *temp;

    char ipstr [INET6_ADDRSTRLEN];
    int socket_fd;

    memset (&hints, 0, sizeof(hints));
    hints.ai_family = AF_UNSPEC;        // Allows both IPv4 and IPv6
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_PASSIVE;        // Allows automatic fill-up of IP (allows using localhost)

    if ((status = getaddrinfo (NULL , "8000", &hints, &res)) != 0 ) {
        cout << "[-] Error: " << gai_strerror (status) << endl;
        exit (EXIT_FAILURE);
    }

    socket_fd = socket (res->ai_family, res->ai_socktype, res->ai_protocol);
    if (socket_fd == -1) {
        cout << "[-] Error : Socket couldn't be created" << endl;
        exit (EXIT_FAILURE);
    }

    if (bind (socket_fd, res->ai_addr, res->ai_addrlen) == -1 ) {
        cout << "[-] Error: Failed to bind to port" << endl;
        exit (EXIT_FAILURE);
    }

    if (connect (socket_fd, res->ai_addr, res->ai_addrlen) == -1) {
        cout << "[-] Error: Failed to connect to remote user" << endl;
        exit (EXIT_FAILURE);
    }

    freeaddrinfo (res);        // free the linked list returned by getaddrinfo ()
    return 0;
}

```

The code is returning :   

[-] Error: Failed to bind to port

It doesn't matter if I put NULL on getaddrinfo ()'s first parameter (making it listen to localHost) or to provide some address, it isn't able to bind port to socket. The port is available as I have tried by changing the value of port. What is wrong with the code ?