To bind socket with localhost, before you invoke the bind function, sin_addr.s_addr field of the sockaddr_in structure should be set properly. The proper value can be obtained either by
my_sockaddress.sin_addr.s_addr = inet_addr("127.0.0.1")
or by
my_sockaddress.sin_addr.s_addr=htonl(INADDR_LOOPBACK);