mongocxx driver tries to connect to a different port

I’m trying to connect to an online MongoDB Atlas cluster using the mongocxx driver. But sometimes, it wouldn’t try to connect to the online server, instead, it tries to connect to port 27017, and it fails. It would work the first time I run my code, then 5 seconds later, it would give the exception, and vice versa. The error is

Exception: No suitable servers found (serverSelectionTryOnce set): [socket timeout calling hello on ‘ac-lda0c27-shard-00-01.m9dq4lf.mongodb.net:27017’]: generic server error

I have had a server run on port 27017 before, but I have since closed it, as nothing shows up with the bash command “lsof -i :27017”, and it’s still like this.

Using the same URI, I’ve been able connect every time on mongosh and the Node.js driver. I don’t understand what’s going on. Can someone help?