can't bind to socket
<p>Code : </p>

<pre><code>#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;unistd.h&gt;
#include &lt;errno.h&gt;
#include &lt;string.h&gt;
#include &lt;netdb.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;netinet/in.h&gt;
#include &lt;sys/socket.h&gt;
// #include &lt;thread&gt;
#include &lt;arpa/inet.h&gt;
#include &lt;iostream&gt;

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 (&amp;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", &amp;hints, &amp;res)) != 0 ) {
        cout &lt;&lt; "[-] Error: " &lt;&lt; gai_strerror (status) &lt;&lt; endl;
        exit (EXIT_FAILURE);
    }

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

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

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

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

<p>The code is returning : <br/>
[-] Error: Failed to bind to port</p>

<p>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 ? </p>