The Java RMI implementation attempts to reuse open sockets where possible for remote invocations. When a remote method is invoked on a stub that uses a custom socket factory, the Java RMI
implementation 'll reuse an open connection if any as long as that socket was created by an equivalent socket factory. Since client socket factories are serialized to clients a single client may have several distinct copies of the same logical socket factory. To ensure that the Java RMI implementation will reuse sockets created by custom socket factories, make sure your custom client socket factory classes implement the hashCode and equals methods appropriately. If the client socket factory does not implement these methods correctly, another ramification is that stubs (using the client socket factory) that refer to the same remote object will not be equal.
The Java RMI implementation attempts to reuse server-side ports as well. It will only do so if there is an existing server socket for the port created by an equivalent socket factory. Make sure the server socket factory class implements the hashCode and equals methods too.
If your socket factory has no instance state a trivial implementation of the hashCode and equals methods are the following:-
public int hashCode() { return 57; }
public boolean equals(Object o) { return this.getClass() == o.getClass() }