title: Getting error code when creating a socket in Linux
body: <p>I'm doing some socket programming in Linux and am wondering how to get the error code when the function socket(...); fails.</p>
  
  <p>for example for the "getaddrinfo" function i can do this:</p>
  
  <pre><code>//Resolve the server address and port
      result = (struct addrinfo *) calloc(1, sizeof(struct addrinfo));
      iResult = getaddrinfo("google.com", DEFAULT_PORT, &amp;hints, &amp;result);
      if (iResult != 0){
          printf("%d\n", iResult);
          fprintf(stderr, "getaddrinfo failed: %s\n", gai_strerror(iResult));
          getchar();
          exit(EXIT_FAILURE);
      }
  </code></pre>
  
  <p>However I want to do a similar thing using socket(...) function.</p>
  
  <p>According to this: 
  <a href="http://linux.die.net/man/2/socket" rel="noreferrer">http://linux.die.net/man/2/socket</a></p>
  
  <p>the function returns -1 on failure, and sets errno to the appropriate error number. How do i access this "errno" though?
  This is my code so far:</p>
  
  <pre><code>int connectSocket = 0;
  connectSocket = socket(AF_INET, SOCK_STREAM, 0);
  printf("%d\n", connectSocket);
  
  if (connectSocket == -1){
      printf("socket failed with error: %s\n", error_string); //TODO: HELP DECLARING error_string
      getchar();
      exit(EXIT_FAILURE);
  }
  </code></pre>