0 votes

I am facing issue "Once the socket has been disconnected, you can only reconnect again asynchronously, and only to a different EndPoint. BeginConnect must be called on a thread that won't exit until the operation has been completed."

Below is my code:

 ProxyFactory factory = new ProxyFactory();
 IProxyClient proxy = factory.CreateProxy(
      ProxyType.Http, uriProxy.Host, uriProxy.Port);

 Socket socket = proxy.Connect(server, Imap.DefaultPort);

 using (Imap imap = new Imap())
 {
     imap.Attach(socket);
     imap.ServerCertificateValidate += Validate;
     imap.ConnectSSL(server);//Port 993                  
     imap.UseBestLogin(username, userpassword);

     // ...

     imap.Close();
 }

Please help.

by (350 points)

1 Answer

0 votes

Don't use ConnectSSL - socket is already connected by using proxy.Connect.

Use AttachSSL (if your server supports SSL or Attach otherwise):

 ProxyFactory factory = new ProxyFactory();
 IProxyClient proxy = factory.CreateProxy(
      ProxyType.Http, uriProxy.Host, uriProxy.Port);

 Socket socket = proxy.Connect(server, Imap.DefaultSSLPort);

 using (Imap imap = new Imap())
 {
     imap.ServerCertificateValidate += Validate;
     imap.AttachSSL(socket);

     imap.UseBestLogin(username, userpassword);

     // ...

     imap.Close();
 }
by (301k points)
edited by
Now I get this error:-
"The handshake failed due to an unexpected packet format."
Any idea where I am wrong.
Does your server support SSL? I think there was a small bug in the code above: Imap.DefaultPort should be: Imap.DefaultSSLPort, if SSL is used.
...