{"record":{"id":"1adc34e82efb56ae","repo":"jstedfast/MailKit","slug":"the-socket-is-not-connected","errorCode":null,"errorMessage":"The socket is not connected.","messagePattern":"The socket is not connected\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"MailKit/Net/Imap/ImapClient.cs","lineNumber":1636,"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 IMAP or IMAP/S server using the provided socket.\n\t\t/// </summary>\n\t\t/// <remarks>\n\t\t/// <para>Establishes a connection to the specified IMAP or IMAP/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>993</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":1618,"sourceCodeEnd":1654,"githubUrl":"https://github.com/jstedfast/MailKit/blob/9d3859a7855e3e17582c07fd01972b8e262bf176/MailKit/Net/Imap/ImapClient.cs#L1618-L1654","documentation":"Thrown as ArgumentException from CheckCanConnect(Socket,...) when a caller passes a Socket to ConnectSocket that is not in a connected state. MailKit requires an already-established socket since it will not dial the endpoint itself.","triggerScenarios":"Passing a freshly created socket without calling Connect(); passing a socket that was closed or reset earlier; passing an unconnected socket from a connection pool.","commonSituations":"Custom socket/proxy setup code that builds the socket but forgets to connect it; reusing a socket after a previous IMAP session dropped it; async connect raced and failed silently before being handed to MailKit.","solutions":["Call socket.Connect(host, port) (or the async equivalent) before passing it to ConnectSocket","Verify socket.Connected is true before handing the socket to MailKit","Create a new socket instead of reusing one that may have been closed"],"exampleFix":"// before\nvar socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);\nclient.ConnectSocket(socket);\n// after\nvar socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);\nsocket.Connect(host, port);\nclient.ConnectSocket(socket);","handlingStrategy":"validation","validationCode":"if (socket == null || !socket.Connected)\n    throw new InvalidOperationException(\"Provide a connected socket\");","typeGuard":"bool IsUsableSocket(Socket s) => s is { Connected: true };","tryCatchPattern":"try { client.ConnectSocket(socket); }\ncatch (ArgumentException) { /* reconnect the socket yourself and retry */ }","preventionTips":["Always socket.Connect(...) immediately before ConnectSocket","Never reuse sockets across IMAP sessions","Check socket.Connected after any async connect await"],"tags":["imap","socket","argument","mailkit"],"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"}