Added encode and decode functions.

Added Flush command function and handle timeout issue.