title: Error when etablishing a TLS connection to smtp.gmail.com
body: <p>I'm trying to connect to smtp.gmail.com with C++ code, and I think I need openSSL. I installed it and successfully connect to server with command line,</p>
  
  <p><code>openssl s_client -starttls smtp -connect smtp.gmail.com:587</code></p>
  
  <p>but I got trouble when establish connection in code, see the last few sentences of the code.</p>
  
  <p>Could anyone please help?</p>
  
  <pre><code>    void main ()
      {
          int err;
          int sd;
          struct sockaddr_in sa;
          SSL_CTX* ctx;
          SSL* ssl;
          X509* server_cert;
          char* str;
          const SSL_METHOD *meth;
  
          WSADATA wsaData;
  
          if(WSAStartup(MAKEWORD(2,2),&amp;wsaData) != 0){
          printf("WSAStartup()fail:%d\n",GetLastError());
          return -1;
          } 
  
          SSL_library_init();
  
          SSL_load_error_strings(); 
  
          meth = TLSv1_2_client_method();
          ctx = SSL_CTX_new (meth); 
          CHK_NULL(ctx);
  
  
          // begin win socket .............................. 
          printf("Begin tcp socket...\n");
  
          sd = socket (AF_INET, SOCK_STREAM, 0); CHK_ERR(sd, "socket");
  
          memset (&amp;sa, 0, sizeof(sa));
          sa.sin_family = AF_INET;
          sa.sin_addr.s_addr = inet_addr (SERVER_ADDR); /* Server IP */
          sa.sin_port = htons (PORT); /* Server Port number */
  
          err = connect(sd, (struct sockaddr*) &amp;sa,  sizeof(sa)); 
          if( sd == SOCKET_ERROR  ) {
          std::cout&lt;&lt;"connect error."&lt;&lt;std::endl;
          return -1;
          }
  
  
          /* SSl negotiation .................. */
          printf("Begin SSL negotiation \n");
  
          ssl = SSL_new (ctx); 
          CHK_NULL(ssl);
  
          SSL_set_fd (ssl, sd);
  
      //ssl connect error here????        
      //What's wrong
  
          err = SSL_connect (ssl);
          if (err == -1)
          {
          ERR_print_errors_fp(stderr);
          }
      }
  </code></pre>