title: Openssl Errors on port change
body: <p>Very simple question. I'm trying to us OpenSSL in Ruby to connect to a service, but getting errors.</p>
  
  <p>When I use the following command:</p>
  
  <pre><code>openssl s_client -ssl3 -showcerts -connect example.com:443 -tls1 -cipher 'DHE-RSA-AES256-SHA' -nbio_test -state
  </code></pre>
  
  <p>It works great! The cert shows up with all the correct information.</p>
  
  <p>But when I do:</p>
  
  <pre><code>openssl s_client -ssl3 -showcerts -connect example.com:13902 -tls1 -cipher 'DHE-RSA-AES256-SHA' -nbio_test -state
  </code></pre>
  
  <p>I get the following errors:</p>
  
  <pre><code>CONNECTED(00000003)
  SSL_connect:before/connect initialization
  SSL_connect:error in SSLv3 write client hello B
  write W BLOCK
  SSL_connect:SSLv3 write client hello B
  SSL3 alert read:fatal:handshake failure
  SSL_connect:failed in SSLv3 read server hello A
  140735228511072:error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure:s3_pkt.c:1275:SSL alert number 40
  140735228511072:error:1409E0E5:SSL routines:SSL3_WRITE_BYTES:ssl handshake failure:s3_pkt.c:598:
  ---
  no peer certificate available
  ---
  No client certificate CA names sent
  ---
  SSL handshake has read 7 bytes and written 0 bytes
  ---
  New, (NONE), Cipher is (NONE)
  Secure Renegotiation IS NOT supported
  Compression: NONE
  Expansion: NONE
  SSL-Session:
      Protocol  : TLSv1
      Cipher    : 0000
      Session-ID: 
      Session-ID-ctx: 
      Master-Key: 
      Key-Arg   : None
      PSK identity: None
      PSK identity hint: None
      SRP username: None
      Start Time: 1408456884
      Timeout   : 7200 (sec)
      Verify return code: 0 (ok)
  ---
  </code></pre>
  
  <p><strong>The only thing that changed between the two commands is the port</strong>. The other flags were copied directly from the successful request. In other words I first did <code>openssl s_client -showcerts -connect example.com:443</code> and then copied the ssl version, tls version, and cipher into the second two commands.</p>
  
  <p>Is this a problem with openssl or the certificate? And if it is a problem with the certificate, how do I fix it? (and what is it?)</p>
  
  <hr>
  
  <p>PS. I was able to connect in Java just fine using the following code, but still have to find a Ruby way:</p>
  
  <pre><code>public static void main(String[] args) throws NoSuchAlgorithmException,
          KeyManagementException, IOException {
  
      TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
          public java.security.cert.X509Certificate[] getAcceptedIssuers() {
              return null;
          }
  
          public void checkClientTrusted(X509Certificate[] certs,
                  String authType) {
          }
  
          public void checkServerTrusted(X509Certificate[] certs,
                  String authType) {
          }
      } };
  
      final SSLContext sc = SSLContext.getInstance("SSL");
      sc.init(null, trustAllCerts, new java.security.SecureRandom());
      HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
  
      HostnameVerifier allHostsValid = new HostnameVerifier() {
          public boolean verify(String hostname, SSLSession session) {
              return true;
          }
      };
  
      HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
  
      URL url = new URL("https://example.com:13902");
      URLConnection con = url.openConnection();
      final Reader reader = new InputStreamReader(con.getInputStream());
      final BufferedReader br = new BufferedReader(reader);
      String line = "";
      while ((line = br.readLine()) != null) {
          System.out.println(line);
      }
      br.close();
  }
  </code></pre>