TLS fails even though the certificate is valid. Can hostname/IP mismatch be the reason?

Yes. If the destination passed to InitializeTls() (hostname or IP) does not match SAN/CN values in the certificate, server identity verification fails and the connection is rejected.

For example, connecting to localhost with a certificate that only includes 127.0.0.1 will fail. Keep the connection identifier and certificate identity aligned.