Let's say my server can support either FTP or SCP, but I don't know which. If it's using SCP and my SecureFTPConnection client is set to the FTP protocol, I will quickly get a SocketException (10061 Connection Refused). This is nice because I can catch the exception, check for the 10061 code, and switch to using SCP instead because I know FTP isn't supported.
Now consider the other direction. The server is using FTP, but my client is set to the SCP protocol. In this case I get an IOException with the message "Failed to connect to 192.168.43.45:22 within timeout 10000 ms" and this exception is actually raised pretty quickly instead of after the full timeout. Not so nice because an IOException can be raised for a multitude of other reasons (actual timeouts because the server is not present, other timeouts like DNS issues, etc). So it's harder to know with the IOException what the true reason is. Did it really time out? Was the protocol unsupported?
So what's the difference? Why do I get an IOException instead of a SocketException which would be more helpful? Is there an easier way to know that the protocol being used isn't supported? My current plan if I get an IOException is to try a basic TCP connection afterwards with a short timeout using a raw socket and catching the 10061 SocketException myself so I know whether the connection is being actively refused. And in this case I'll actually ask the user if they want to use an unsecure connection instead. But it would sure be nice if I knew this from the SecureFTPConnection client itself.
FYI, I think I also get the same scenarios when choosing between SFTP and FTP, but I haven't been able to fully test this.