{"record":{"id":"f7fe1cf3cfcb7261","repo":"jstedfast/MailKit","slug":"the-socket-is-not-connected-pop3client","errorCode":null,"errorMessage":"The socket is not connected.","messagePattern":"The socket is not connected\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Pop3/Pop3Client.cs","lineNumber":1274,"sourceCode":"\t\t\t\tthrow;\n\t\t\t}\n\t\t}\n\n\t\tvoid CheckCanConnect (Stream stream, string host, int port)\n\t\t{\n\t\t\tif (stream == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (stream));\n\n\t\t\tCheckCanConnect (host, port);\n\t\t}\n\n\t\tvoid CheckCanConnect (Socket socket, string host, int port)\n\t\t{\n\t\t\tif (socket == null)\n\t\t\t\tthrow new ArgumentNullException (nameof (socket));\n\n\t\t\tif (!socket.Connected)\n\t\t\t\tthrow new ArgumentException (\"The socket is not connected.\", nameof (socket));\n\n\t\t\tCheckCanConnect (host, port);\n\t\t}\n\n\t\t/// <summary>\n\t\t/// Establish a connection to the specified POP3 or POP3/S server using the provided socket.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// <para>Establishes a connection to the specified POP3 or POP3/S server using\n\t\t/// the provided socket.</para>\n\t\t/// <para>If the <paramref name=\"options\"/> has a value of\n\t\t/// <see cref=\"SecureSocketOptions.Auto\"/>, then the <paramref name=\"port\"/> is used\n\t\t/// to determine the default security options. If the <paramref name=\"port\"/> has a value\n\t\t/// of <c>995</c>, then the default options used will be\n\t\t/// <see cref=\"SecureSocketOptions.SslOnConnect\"/>. All other values will use\n\t\t/// <see cref=\"SecureSocketOptions.StartTlsWhenAvailable\"/>.</para>\n\t\t/// <para>Once a connection is established, properties such as\n\t\t/// <see cref=\"AuthenticationMechanisms\"/> and <see cref=\"Capabilities\"/> will be","sourceCodeStart":1256,"sourceCodeEnd":1292,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Pop3/Pop3Client.cs#L1256-L1292","documentation":"Pop3Client.Connect(socket, host, port) validates the supplied Socket argument and throws this ArgumentException when socket.Connected is false. MailKit requires a pre-established TCP connection; it will not dial the endpoint itself when given a socket. The error means the caller passed a socket that was never connected or has been closed/disconnected.","triggerScenarios":"Passing a Socket created but never bound/connected; passing a socket whose remote peer already closed it (Connected becomes false); passing a closed socket after a failed or completed previous session.","commonSituations":"Custom connection-pooling code returning disconnected sockets; reusing a socket after Disconnect(); race where the socket died before being handed to Pop3Client; forgetting to call socket.Connect() first.","solutions":["Establish the connection first: socket.Connect(host, port) (or ConnectAsync) before passing it to Pop3Client.Connect.","Check socket.Connected before handing the socket to the client and reconnect if false.","If pooling sockets, validate/refresh them on checkout and discard dead ones.","Create a fresh socket instead of reusing one from a closed session."],"exampleFix":"// before\nvar socket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);\nclient.Connect (socket, \"pop.example.com\", 995);\n// after\nvar socket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);\nsocket.Connect (\"pop.example.com\", 995);\nclient.Connect (socket, \"pop.example.com\", 995);","handlingStrategy":"validation","validationCode":"if (socket == null || !socket.Connected)\n    throw new InvalidOperationException (\"Socket must be connected before passing to Pop3Client.Connect\");","typeGuard":null,"tryCatchPattern":"try {\n    client.Connect (socket, host, port, options, cancellationToken);\n} catch (ArgumentException ex) when (ex.ParamName == \"socket\") {\n    // socket not connected: reconnect and retry once\n    socket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);\n    socket.Connect (host, port);\n    client.Connect (socket, host, port, options, cancellationToken);\n}","preventionTips":["Always call socket.Connect/ConnectAsync before handing the socket to Pop3Client","Check socket.Connected on checkout when reusing pooled sockets","Never reuse a socket after client.Disconnect(); create a fresh one"],"tags":["pop3","socket","argument","mailkit","tcp"],"backgroundTag":"invalid-argument-value","analyzedSha":"9d3859a7855e3e17582c07fd01972b8e262bf176","analyzedAt":"2026-09-15T15:46:11.592Z","contentChangedAt":"2026-09-15T15:46:11.592Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}