diff --git a/src/TCP/connection.cpp b/src/TCP/connection.cpp index 34bcb2a..8d6fbd7 100644 --- a/src/TCP/connection.cpp +++ b/src/TCP/connection.cpp @@ -204,8 +204,10 @@ Connection Connection::with(std::string addr, int port) { server.sin_port = ::htons(port); server.sin_addr = {inet_addr(addr.c_str())}; - if (::connect(sock, reinterpret_cast(&server), sizeof(server)) < 0) + if (::connect(sock, reinterpret_cast(&server), sizeof(server)) < 0) { + ::close(sock); throw std::runtime_error("Cannot connect, errno = " + std::to_string(errno)); + } return Connection(sock); }