Add encode and decode function changes

add encode and decode functions
add flush command and handled timeout issue