After looking through the code a bit, I think you can solve your issue using the imap_init plugin and one minor core change. All in all it looks like an hours coding and 3 hours testing.
$GLOBALS['IMAP']->conn->fp = $imapsocket;
3)set the logged property on connection so it knows it is connected
$GLOBALS['IMAP']->conn->logged = TRUE;
Now for the core code change:In the imap library, the connect code is a do/while loop. This means it always tries to connect at least once, even if you are already connected. And while it is possible to force the imap_generic option to abort the connection attempt by setting userid or password to nothing, it won't abort before setting logged to false, so roundcube will not think it is logged in. Change that from a do/while loop to a while loop[move the condition to the top], or surround it by an if not connected clause, and you can bypass processing if you have already connected.
/program/include/rcube_imap.php
do {
$data = rcmail::get_instance()->plugins->exec_hook('imap_connect',
array('host' => $host, 'user' => $user, 'attempt' => ++$attempt));
if (!empty($data['pass']))
$pass = $data['pass'];
$this->conn->connect($data['host'], $data['user'], $pass, $this->options);
} while(!$this->conn->connected() && $data['retry']);