diff --git a/server/src/main/java/org/red5/server/CoreHandler.java b/server/src/main/java/org/red5/server/CoreHandler.java
index 710eda38054a49ef8fc8ad26bd9770e8d4ae11aa..887c3cc600a88445ac70e1a1ece5d6d4d61273cc 100644
--- a/server/src/main/java/org/red5/server/CoreHandler.java
+++ b/server/src/main/java/org/red5/server/CoreHandler.java
@@ -47,6 +47,49 @@ public class CoreHandler implements IScopeHandler, CoreHandlerMXBean {
         return connect(conn, scope, null);
     }
 
+        /** Auxiliary function to connect
+     * 
+     * Creates a client if needed then sets the client on the connection
+     * 
+     * @param client
+     *            the connected client
+     * @param clientRegistry
+     *            the client's registry
+     * @param conn
+     *            Client connection
+     * @param params
+     *            Parameters passed from client side with connect call
+     * @return the connection with an updated client
+    */
+    private IConnection connectionHandleClient(IClient client, IClientRegistry clientRegistry, IConnection conn, Object[] params) {
+        if (client == null) {
+            if (!clientRegistry.hasClient(conn.getSessionId())) {
+                if (conn instanceof RTMPTConnection) {
+                    log.debug("Creating new client for RTMPT connection");
+                    // create a new client using the session id as the client's id
+                    client = new Client(conn.getSessionId(), (ClientRegistry) clientRegistry);
+                    clientRegistry.addClient(client);
+                    // set the client on the connection
+                    conn.setClient(client);
+                } else if (conn instanceof RTMPConnection) {
+                    log.debug("Creating new client for RTMP connection");
+                    // this is a new connection, create a new client to hold it
+                    client = clientRegistry.newClient(params);
+                    // set the client on the connection
+                    conn.setClient(client);
+                }
+            } else {
+                client = clientRegistry.lookupClient(conn.getSessionId());
+                conn.setClient(client);
+            }
+        } else {
+            // set the client on the connection
+            conn.setClient(client);
+        }
+        return conn;
+    }
+
+
     /**
      * Connects client to the scope
      *
@@ -80,30 +123,10 @@ public class CoreHandler implements IScopeHandler, CoreHandlerMXBean {
             log.debug("Client registry: {}", (clientRegistry == null ? "is null" : "not null"));
             if (clientRegistry != null) {
                 IClient client = conn.getClient();
-                if (client == null) {
-                    if (!clientRegistry.hasClient(id)) {
-                        if (conn instanceof RTMPTConnection) {
-                            log.debug("Creating new client for RTMPT connection");
-                            // create a new client using the session id as the client's id
-                            client = new Client(id, (ClientRegistry) clientRegistry);
-                            clientRegistry.addClient(client);
-                            // set the client on the connection
-                            conn.setClient(client);
-                        } else if (conn instanceof RTMPConnection) {
-                            log.debug("Creating new client for RTMP connection");
-                            // this is a new connection, create a new client to hold it
-                            client = clientRegistry.newClient(params);
-                            // set the client on the connection
-                            conn.setClient(client);
-                        }
-                    } else {
-                        client = clientRegistry.lookupClient(id);
-                        conn.setClient(client);
-                    }
-                } else {
-                    // set the client on the connection
-                    conn.setClient(client);
-                }
+                
+                // call connectionHandleClient to handle all the cases for this connection
+                conn = connectionHandleClient(client, clientRegistry, conn, params);
+                
                 // assign connection to client
                 conn.initialize(client);
                 // we could checked for banned clients here