title: cpp connect() function is blocked indefinitely
body: <pre><code>#include &lt;sys/socket.h&gt;
  #include &lt;cstdio&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;iostream&gt;
  #include &lt;errno.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;arpa/inet.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;unistd.h&gt;
  #define BUFSIZE 5000
  #define PORT 80
  using namespace std;
  int main(int argc, char* argv[]){
      char buffer[BUFSIZE];
      int sockfd;
      struct sockaddr_in their_addr;
      if((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1){
          perror("Socket generating failed");
          exit(1);
      }
      their_addr.sin_family = AF_INET;
      their_addr.sin_port = htons(PORT);
      their_addr.sin_addr.s_addr = htonl(((struct in_addr*)gethostbyname("www.google.com")-&gt;h_addr_list[0])-&gt;s_addr);
      if(connect(sockfd, (struct sockaddr*)&amp;their_addr, sizeof(struct sockaddr)) == -1){ // stops at here!
          perror("Connection failed");
          exit(1);
      }
      close(sockfd);
      return 0;
  </code></pre>
  
  <p>As a test of a combination of gethostbyname + connect, I wrote a simple code.
  It queries the IP address of google by means of gethostbyname(), connects to google do nothing, and close the socket.
  However, the program just stops at a line of the connect() function without any error including perror(). 
  How can I fix this?</p>