not sure, but maybe you could split this function in 2 function, one for the connection handling and the other for the vj.