--- ftp.erl	Tue Aug 23 18:39:01 2005
+++ /home/serge/Projects/DRP/lib/drpdb-1.0/src/ftp.erl	Tue Aug 23 18:40:12 2005
@@ -707,12 +707,18 @@
     {noreply, State#state{data = <<(State#state.data)/binary,
 				  Data/binary>>}};
 
-handle_info({tcp_closed, Socket}, #state{dsock = Socket, client = From,
+handle_info({tcp, Socket, Data}, #state{dsock = {lsock, Socket}} = State) ->
+    activate_data_connection(State),
+    {noreply, State#state{data = <<(State#state.data)/binary,
+                  Data/binary>>}};
+
+handle_info({tcp_closed, Socket}, #state{dsock = Socket, % client = From,
 					 caller = {recv_file, Fd}} 
 	    = State) ->
     file_close(Fd),
-    gen_server:reply(From, ok),
-    {noreply, State#state{dsock = undefined, client = undefined,
+    activate_ctrl_connection(State),
+    % gen_server:reply(From, ok),
+    {noreply, State#state{dsock = undefined, % client = undefined,
 			  data = <<>>, caller = undefined}};
 
 handle_info({tcp_closed, Socket}, #state{dsock = Socket, client = From,
@@ -790,11 +796,11 @@
 %% terminate/2 and code_change/3
 %%--------------------------------------------------------------------------
 terminate(normal, State) ->
-    do_termiante({error, econn}, State);
+    do_terminate({error, econn}, State);
 terminate(Reason, State) ->
-    do_termiante({error, Reason}, State).
+    do_terminate({error, Reason}, State).
 
-do_termiante(ErrorMsg, State) ->
+do_terminate(ErrorMsg, State) ->
     close_data_connection(State),
     close_ctrl_connection(State),
     case State#state.client of
@@ -1094,11 +1100,16 @@
     activate_data_connection(NewState),
     {noreply, NewState};
 
+handle_ctrl_result({pos_compl, _Lines}, #state{caller = unknown, client = From} = State) ->
+    close_data_connection(State),
+    gen_server:reply(From, ok),
+    {noreply, State#state{client = undefined}};
+
 handle_ctrl_result({Status, _}, #state{caller = {recv_file, Fd}} = State) ->
     file_close(Fd),
     close_data_connection(State),
-    ctrl_result_response(Status, State#state{dsock = undefined}, 
-			 {error, epath});
+    ctrl_result_response(Status, State#state{dsock = undefined}, ok);
+
 %%--------------------------------------------------------------------------
 handle_ctrl_result({pos_prel, _}, #state{caller = {transfer_file, Fd}} 
 		   = State) ->
@@ -1300,6 +1311,9 @@
 activate_ctrl_connection(#state{csock = Socket}) ->
     activate_connection(Socket).
 
+activate_data_connection(#state{dsock = {lsock, Socket}}) ->
+    activate_connection(Socket);
+
 activate_data_connection(#state{dsock = Socket}) ->
     activate_connection(Socket).
 
@@ -1313,6 +1327,8 @@
 
 close_data_connection(#state{dsock = undefined}) ->
     ok;
+close_data_connection(#state{dsock = {lsock, Socket}}) ->
+    close_connection(Socket);
 close_data_connection(#state{dsock = Socket}) ->
     close_connection(Socket).
 
