title: Unable to accept connections on socket, when creating sockets on remote node via RPC in Erlang
body: <p>I am struggling to identify the reason for gen_tcp:accept always returning an {error, closed} response.</p>
  
  <p>Essentially, I have a supervisor that creates a listening socket:</p>
  
  <pre><code>gen_tcp:listen(8081, [binary, {packet, 0}, {active, false}, {reuseaddr, true}]),
  </code></pre>
  
  <p>This socket is then passed to a child, which is an implementation of the gen_server behaviour. The child then accepts connections on the socket.</p>
  
  <pre><code>accept(ListeningSocket, {ok, Socket}) -&gt;                                   
      spawn(fun() -&gt; loop(Socket) end),                                      
      accept(ListeningSocket);
  accept(_ListeningSocket, {error, Error}) -&gt;
      io:format("Unable to listen on socket: ~p.~n", [Error]),
      gen_server:call(self(), stop).
  
  accept(ListeningSocket) -&gt;                                                 
      accept(ListeningSocket, gen_tcp:accept(ListeningSocket)).                                                                                             
  
  loop(Socket) -&gt;                                                            
      case gen_tcp:recv(Socket, 0) of                                        
          {ok, Data} -&gt;                                                      
              io:format("~p~n", [Data]),                                     
              process_request(Data),                                         
              gen_tcp:send(Socket, Data),                                    
              loop(Socket);                                                  
          {error, closed} -&gt; ok                                              
     end.
  </code></pre>
  
  <p>I load the supervisor and gen_server BEAM binaries locally, and load them on a another node (which runs on the same machine) via an RPC call to code:load_binary.
  Next, I execute the supervisor via an RPC call, which in turn starts the server.{error, closed} is always returned by gen_tcp:accept in this scenario.</p>
  
  <p>Should I run the supervisor and server while logged in to a node shell, then the server can accept connections without issue. This includes 'remsh' to the remote node that would fail to accept connections, had I previously RPCed it to start the server unsuccessfully.</p>
  
  <p>I seem to be able to replicate the issue by using the shell alone:</p>
  
  <pre><code>[Terminal 1]: erl -sname node -setcookie abc -distributed -noshell
  
  [Terminal 2]: erl -sname rpc -setcookie abc:
  
                net_adm:ping('node@verne').
                {ok, ListeningSocket} = rpc:call('node@verne', gen_tcp, listen, [8081, [binary, {packet, 0}, {active, true}, {reuseaddr, true}]]).
                rpc:call('node@verne', gen_tcp, accept, [ListeningSocket]).
  </code></pre>
  
  <p>The response to the final RPC is {error, closed}.</p>
  
  <p>Could this be something to do with socket/port ownership?</p>
  
  <p>In case it is of help, there are no clients waiting to connect, and I don't set timeouts anywhere.</p>