It says it disconnected you because your connection was inactive for 300 seconds. This is a common practice on FTP servers since (as opposed to HTTP) FTP is not stateless, connections stay open and connections that do nothing can easily fill the connection limit of the server.
The obvious solution is making sure you don't stay inactive for 300 seconds. Create a timer that does something every minute or so, like getting a list of files in the current directory or something.
EDIT: As ChaosPandion mentionned in a comment, maybe you should just close the connection when you're done and reopen it when you need it again.