The best way to ensure that you keep your nick across connections, and that no one else can grab it out from under you, is to register it with the NickServ bot on your IRC server, and then authenticate to NickServ when you log in.
To register your nick, which needs doing only once, you'd:
/msg nickserv register [nick] [password]
Thereafter, when logging in, do:
/nick [nick] /msg nickserv identify [password]
(If you don't identify to NickServ within a short time after setting your nick, it will automatically change your nick to something else, as it doesn't know you're the one who registered that nick until you tell it so with the identify
command.)
When logging on after forcible disconnection, as in the case which prompted your question, you can have NickServ clean up your previous connection so that you can reclaim your nick, and this is done as:
/msg nickserv ghost [nick] [password] /nick [nick]
(You may or may not need to identify
after the /nick
command here; I tend to think not, since you've just implicitly done so in the ghost
command, but IIRC different NickServ instances behave differently in this case. In any case, if NickServ requires that you identify after setting your nick, it should let you know.)
For more info on how to use NickServ's rather impressive facilities, see here.